Announcement Announcement Module
Collapse
No announcement yet.
AOP not working at all (on a public interface method, with aop:aspectj-autoproxy) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP not working at all (on a public interface method, with aop:aspectj-autoproxy)

    Strange, i made it work a few months ago but now i can't get even a basic Aspect to work. No methods are ever intercepted, as if i had no AOP at all.

    The configuration:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        
        <aop:aspectj-autoproxy/>
        <bean class="hellotrackworld.AspectTata"/>
        
        <context:component-scan base-package="hellotrackworld"/>
        <context:annotation-config/>
        
        
    </beans>
    The aspect (the poincut seems alright)
    Code:
    package hellotrackworld;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.jboss.aop.Aspect;
    
    
    @Aspect
    public class AspectTata
    {
    	@Before ( "anyPublicOperation()" )
    	public void printTata ()
    	{
    		System.out.println ( "!!!!!TATA!!!!!!" );
    		System.out.println ( "!!!!!TATA!!!!!!" );
    		System.out.println ( "!!!!!TATA!!!!!!" );
    		System.out.println ( "!!!!!TATA!!!!!!" );
    		System.out.println ( "!!!!!TATA!!!!!!" );
    	}
    	
    	@SuppressWarnings("unused")
    	@Pointcut("execution(public * *(..))")
        private void anyPublicOperation() {}
    }
    The bean that should be advised:
    Code:
    package hellotrackworld;
    
    import org.springframework.stereotype.Service;
    
    @Service("fooService")
    public class FooServiceImpl implements FooService 
    {
    	@Override
    	public void foo() 
    	{
    		System.out.println ( "foo" );
    	}
    }
    The test:
    Code:
    package hellotrackworld;
    
    
    
    import javax.annotation.Resource;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.aop.support.AopUtils;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration ( locations={"/resources/application-context.xml"} )
    public class TestAOP
    {
    	@Resource ( name="fooService" ) 
    	private FooService fooService;
    
    	@Test
    	public void toto ()
    	{
    		boolean isAopProxy;
    		isAopProxy = AopUtils.isAopProxy(fooService);//returns false
    		System.out.println ( "______isAopProxy: " + isAopProxy );
    		
    		fooService.foo();//no interception
    	}
    }

  • #2
    Provide FooService source code as well.

    Try to use '<aop:aspectj-autoproxy proxy-target-class="true"/>'.

    Comment


    • #3
      Specifying proxy-target-class="true" doesn't work either.

      The bean interface:
      Code:
      package hellotrackworld;
      
      public interface FooService 
      {
      	void foo ();
      }

      Comment


      • #4
        Just tried your example and everything works fine for me.

        The problem was that you used invalid @Aspect annotation ('org.jboss.aop.Aspect' instead of 'org.aspectj.lang.annotation.Aspect').

        Comment


        • #5
          Oh..
          Thanks for spotting that, i wouldn't have noticed. Now it works just fine.

          Comment


          • #6
            Same problem, no solution

            Code:
            import org.apache.log4j.Logger;
            import org.aspectj.lang.JoinPoint;
            import org.aspectj.lang.annotation.Aspect;
            import org.aspectj.lang.annotation.Before;
            import org.aspectj.lang.annotation.Pointcut;
            
            @Aspect
            public class PropertyTracker {
            	
            	private Logger logger = Logger.getLogger(this.getClass());
            
            	@Before("updateMethod()")
            	public void trackChange(JoinPoint point){
            		String name = point.getSignature().getName();
            		Object newValue = point.getArgs()[0];
            		logger.info(name + " is about to change to "+newValue+" on object "+point.getTarget());
            		System.out.println(name + " is about to change to "+newValue+" on object "+point.getTarget());
            	}
            	
            	@Pointcut("execution(* update(*))")
            	public void updateMethod(){}
            }
            I can see in bean cross reference that it matches my target, but nothing append in output (no stdout, no logger info)

            The Spring AOP Event Trace says :

            Code:
            14:41:51 Skipping bean definition [propertyTracker (16) [com.mobiletoken.aspect.PropertyTracker]]
            But also:
            Code:
            14:41:51 Created AOP reference [Bean definition [1:BeansModel|2:mtug|3:src/test/java/com/mobiletoken/action/system-test-config.xml|5:classpath:com/mobiletoken/application-config.xml:10|3:src/main/java/com/mobiletoken/application-config.xml|7:context:component-scan:12|8:hibernateActionManager:19] advise target [void update(Action) [in HibernateActionManager [in [Working copy] HibernateActionManager.java [in com.mobiletoken.entitymanager.internal [in src/main/java [in mtug]]]]]] advise source [void trackChange(JoinPoint) [in PropertyTracker [in [Working copy] PropertyTracker.java [in com.mobiletoken.aspect [in src/main/java [in mtug]]]]] ] aspect definition [Aspect definition [\mtug\src\main\java\com\mobiletoken\application-config.xml:17] advise type [before] advise [com.mobiletoken.aspect.PropertyTracker.trackChange(org.aspectj.lang.JoinPoint)]]]
            Can anyone help?

            Comment


            • #7
              You didn't provide enough information. Create complete standalone test-case that illustrates the problem and post it here.

              Comment


              • #8
                See my new thread ref 76391 with complete case.

                http://forum.springsource.org/showthread.php?t=76391

                thank you for your reply

                Comment

                Working...
                X