Results 1 to 6 of 6

Thread: @Before AOP not working

  1. #1
    Join Date
    Jan 2013
    Posts
    5

    Default @Before AOP not working

    Hi,

    I am trying to write a simple standalone program using the @before annotation of Spring AOP, but it is not working as expected. I am posting below the classes and Beans.xml file that i have generated for the same. THis is a very basic implementation but can't understand why is not working. Any help is much appreciated

    Aspect Class:
    Code:
    package com.royz.test.aop;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    
    @Aspect
    public class LoggerAspect {
    	
    	@Before("execution(* com.royz.test.Performer.playTune(..))")
    	public void logplaytune(){
    		System.out.println("Is about to play  tune");
    	}
    }
    Interface which needs to be advised

    package com.royz.test;

    Code:
    public interface Performer {
    	
    	public void playTune();
    	public void bowToApplause();
    	public void playtuneThrowsExcepttion() throws Exception;
    
    }
    Implementation of the Interface

    Code:
    package com.royz.test;
    
    public class PerformerImpl implements Performer {
    
    	public void playTune() {
    		System.out.println("Playing tune");
    
    	}
    
    	public void bowToApplause() {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void playtuneThrowsExcepttion() throws Exception {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    The class which is launching the Spring container
    Code:
    package com.royz.test;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.FileSystemResource;
    
    public class SprintTestLaunch {
    
    	public static void main(String[] args) {
    		
    		BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("E:\\xyz\\Spring STS\\workspace\\testaop\\src\\main\\java\\com\\royz\\test\\beans.xml"));
    		Performer prfmr = (Performer)beanFactory.getBean("performer");
    		prfmr.playTune();
    	}
    }
    The Beans.xml file
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    	http://www.springframework.org/schema/aop 
    	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
     
    	<aop:aspectj-autoproxy />
     
    	<bean id="performer" class="com.royz.test.PerformerImpl" />
    	<!-- Aspect -->
    	<bean id="logAspect" class="com.royz.test.aop.LoggerAspect" />
     
    </beans>
    When i am running this code the output i am getting is 'playing tune' but i am not getting the message that should be printed as part of the @Before advice.

    Regards,
    Royz

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Please use the search as this question has been answered numerous times before, also a read of chapter 3 of the reference guide is suggested...

    Don't use a BeanFactory use an ApplicationContext implementation.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2010
    Posts
    28

    Default

    Marten Deinum,I use Annotation-based config in my project,i use @Service Annotation on my service layer to inject bean,such as @Service on the above com.royz.test.PerformerImpl then the @AspectJ not working.I need to add @Component on the AspectJ Bean such as above LoggerAspect,But in the tutorial on springSource blog:http://blog.springsource.org/2007/05...-spring-21/,it said
    And to activate automatic proxy generation, simply add the following tag to the xml:
    <aop:aspectj-autoproxy/>
    So, When project is Annotation-based,@AspectJ Annotation not work? Or i miss somewhere config?

    Quote Originally Posted by Marten Deinum View Post
    Please use the search as this question has been answered numerous times before, also a read of chapter 3 of the reference guide is suggested...

    Don't use a BeanFactory use an ApplicationContext implementation.

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    What Marten is saying is that Spring AOP only works if you use ApplicationContext. You are using BeanFactory. Change this:

    Code:
    BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("E:\\xyz\\Spring STS\\workspace\\testaop\\src\\main\\java\\com\\royz\\test\\beans.xml"));
    to this:

    Code:
    ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/royz/test/beans.xml");
    ctx.getBean()...
    and you should be ok.

  5. #5
    Join Date
    Jul 2010
    Posts
    28

    Default

    Sorry,I use annotation-base in my project,not xml config,and i do not use beanfactory or applicationcontext in my project,so you solution is not for my issue.
    Quote Originally Posted by Enrico Pizzi View Post
    What Marten is saying is that Spring AOP only works if you use ApplicationContext. You are using BeanFactory. Change this:

    Code:
    BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("E:\\xyz\\Spring STS\\workspace\\testaop\\src\\main\\java\\com\\royz\\test\\beans.xml"));
    to this:

    Code:
    ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/royz/test/beans.xml");
    ctx.getBean()...
    and you should be ok.

  6. #6

    Default

    hello gang

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •