Announcement Announcement Module
Collapse
No announcement yet.
@Before AOP not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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
    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.

    Comment


    • #3
      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?

      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.

      Comment


      • #4
        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.

        Comment


        • #5
          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.
          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.

          Comment


          • #6
            hello gang

            Comment

            Working...
            X