Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Can't get advice in the simplest example to execute

  1. #1
    Join Date
    Dec 2006
    Location
    Nottingham, UK
    Posts
    39

    Question Can't get advice in the simplest example to execute

    I have managed to get Introductions to work, in that an introduced concrete method does run.
    I've included that as well as the failing advice so you can see that most of my cofig and setup must be correct... I wish!!
    What is the thing I'm doing wrong?

    To add to that - I can only get the following code to work for Introductions when I do not declare @Aspect in the class I want advising (MyWorld). Which sounds right.
    But I can only get myPointcut declaration to be resolved if I put @Aspect back in. i.e.
    @AfterReturning("timeout.helloworld.AnotherAspect. myPointcut()")
    Does not raise an error, otherwise without the @Aspect I get:
    error at ::0 can't find referenced pointcut myPointcut

    There isn't this problem when I declare the Pointcut in place.

    But anyway at no point am I able to see the Advice run during any combination of stuff I've tried. ie I never see output of:

    ""Here is my advice"

    There seems so few worked examples out there to look at.

    I'd be very grateful if some kind soul could put me on track.

    HTML 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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    	
    	<aop:aspectj-autoproxy />
    
    	<bean id="anotherAspect" class="timeout.helloworld.AnotherAspect"></bean>
    	<bean id="helloAspect" class="timeout.helloworld.HelloAspect"></bean>
    	<bean id="myWorld" class="timeout.helloworld.worlds.MyWorld"></bean>
    </beans>
    Code:
    package timeout.helloworld;
    
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.DeclareParents;
    
    @Aspect
    public class HelloAspect {
    	
    	@DeclareParents(value="timeout.helloworld.worlds.*+",
    		defaultImpl=timeout.helloworld.DefaultWorld.class)
    	public static World mixin;
    	
    	
    	/*
    	 * Execute the following Advice after any method returns
    	 */
    	//@AfterReturning("timeout.helloworld.AnotherAspect.myPointcut()")
    	@AfterReturning("execution (* *(..))")
    	public void myAdvice() {
    		System.out.println("Here is my advice");
    		
    	}	
    	
    	public void testAdvice() {	
    		System.out.println("HelloAspect.testAdvice()");
    	}
    		
    }
    Code:
    package timeout.helloworld.worlds;
    
    import org.aspectj.lang.annotation.Aspect;
    
    //@Aspect // put it in take it out shake it all about :-(
    public class MyWorld {
    	
    	public String doSomut() {
    		return "My World Doing something!!!!!!!!!";
    	}
    	
    }
    
    package timeout.helloworld;
    
    public class DefaultWorld implements World {
    	
    	public String doSomut() {	
    		return "DefaultWorld does something!";
    		
    	}
    }
    
    package timeout.helloworld;
    
    public interface World {
    	
    	String doSomut();
    }
    Code:
    package timeout.helloworld;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    @Aspect
    public class HelloRunner {
    
    	public static void main(String[] args) {
    		
    		BeanFactory factory = new FileSystemXmlApplicationContext(
    			"build/WEB-INF/classes/applicationContext/test-aspects.xml");
    		
    		World world = (World) factory.getBean("myWorld");
    		
    		System.out.println(world.doSomut());
    	
    		AnotherAspect obj2 = (AnotherAspect) factory.getBean("anotherAspect");		
    		obj2.testAdvice();
    		HelloAspect obj3 = (HelloAspect) factory.getBean("helloAspect");		
    		obj3.testAdvice();
    		
    		new HelloRunner().testAdvice();
    	}
    	
    	public void testAdvice() {
    		
    		System.out.println("HelloRunner.testAdvice()");
    	}
    }

  2. #2
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    What does your AnotherAspect look like?

    Here are a few pointers:
    1. AnotherAspect must be marked @Aspect and it must contain a method maned myPointcut() marked @Pointcut
    2. You don't need @Aspect annotations for any of the MyWorld, DefaultWorld, and World types.

    May I also suggest that you start using more descriptive pointcut and aspect names? I realize that you are just exploring, but it still is helpful to a reader -- who may well be you :-). For example, myPointcut() may be better named as anyMethod().

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  3. #3
    Join Date
    Dec 2006
    Location
    Nottingham, UK
    Posts
    39

    Default

    Quote Originally Posted by ramnivas View Post
    What does your AnotherAspect look like?
    Damn, I missed that one out

    Code:
    package timeout.helloworld;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class AnotherAspect {
    	
    	public void doAccessCheck() {}
    	
    	@Pointcut("execution(* *(..))")
    	public void myPointcut() {}
    	
    	public void testAdvice() {	
    		System.out.println("AnotherAspect.testAdvice()");
    	}
    	
    }
    Here are a few pointers:
    1. AnotherAspect must be marked @Aspect and it must contain a method maned myPointcut() marked @Pointcut
    Yes as you can see it does.


    2. You don't need @Aspect annotations for any of the MyWorld, DefaultWorld, and World types.
    That is what I expected, but I get the behaviour as noted when adding or removing the @Aspect annotation from MyWorld


    May I also suggest that you start using more descriptive pointcut and aspect names? I realize that you are just exploring, but it still is helpful to a reader -- who may well be you :-). For example, myPointcut() may be better named as anyMethod().

    -Ramnivas
    Well in this case it would be better named somePointcutIDontCareWhatButPleaseDoSomethingAnyth ingPrettyPlease()

    So taking what you've said on board and removing the unneccessary @Aspect I get no errors but here is the Spring output:

    HTML Code:
    17-Dec-2006 13:07:57 org.springframework.core.CollectionFactory <clinit>
    INFO: JDK 1.4+ collections available
    17-Dec-2006 13:07:57 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from file [D:\workspace\Timeout\build\WEB-INF\classes\applicationContext\test-aspects.xml]
    17-Dec-2006 13:07:57 org.springframework.context.support.AbstractRefreshableApplicationContext refreshBeanFactory
    INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=25253977]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,anotherAspect,helloAspect,myWorld,helloRunner,defaultWorld]; root of BeanFactory hierarchy
    17-Dec-2006 13:07:57 org.springframework.context.support.AbstractApplicationContext refresh
    INFO: 6 beans defined in application context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=25253977]
    17-Dec-2006 13:07:57 org.springframework.aop.framework.DefaultAopProxyFactory <clinit>
    INFO: CGLIB2 available: proxyTargetClass feature enabled
    17-Dec-2006 13:07:57 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
    INFO: Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    17-Dec-2006 13:07:57 org.springframework.context.support.AbstractApplicationContext initMessageSource
    INFO: Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@1bcdbf6]
    17-Dec-2006 13:07:57 org.springframework.context.support.AbstractApplicationContext initApplicationEventMulticaster
    INFO: Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@16f25a7]
    17-Dec-2006 13:07:57 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [org.springframework.aop.config.internalAutoProxyCreator,anotherAspect,helloAspect,myWorld,helloRunner,defaultWorld]; root of BeanFactory hierarchy]
    
    DefaultWorld does something!
    AnotherAspect.testAdvice()
    HelloAspect.testAdvice()
    HelloRunner.testAdvice()
    So the Introduction works but I'm also expecting the output of
    "Here is my advice" for every method I call.

    Thanks for your help, but I'm still lost.
    Last edited by visionset; Dec 17th, 2006 at 07:11 AM. Reason: Apending

  4. #4
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    Here is a potential bug in Spring:
    - Aspects are not advised.

    Can you file a JIRA issue (a JUnit testcase will be appreciated)? If you do so, please notify here.

    Make the following modifications to your program and see
    your bean being advised:
    1. Remove @Aspect from HelloRunner.
    2. Add the following code in the main() method:
    Code:
    HelloRunner obj4 = (HelloRunner) factory.getBean("helloRunner");		
    obj4.testAdvice();
    and a bean definition in your XML file:
    Code:
    <bean id="helloRunner" class="timeout.helloworld.HelloRunner"></bean>
    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  5. #5
    Join Date
    Dec 2006
    Location
    Nottingham, UK
    Posts
    39

    Default JIRA placed

    Quote Originally Posted by ramnivas View Post
    Here is a potential bug in Spring:
    - Aspects are not advised.

    Can you file a JIRA issue (a JUnit testcase will be appreciated)? If you do so, please notify here.
    I have done so, though not in JUnit format.


    Make the following modifications to your program and see
    your bean being advised:
    1. Remove @Aspect from HelloRunner.
    2. Add the following code in the main() method:
    Code:
    HelloRunner obj4 = (HelloRunner) factory.getBean("helloRunner");		
    obj4.testAdvice();
    and a bean definition in your XML file:
    Code:
    <bean id="helloRunner" class="timeout.helloworld.HelloRunner"></bean>
    Yes I have already done that and it made no difference.
    If this is a bug, I'm frankly amazed! How can it have got passed to final release?
    I'm still sure I've done something daft.
    I have noticed in the past with Spring that a missed JAR does not always give the signature CNFException.

    So here are my Spring related classpath entries:

    Code:
    aopalliance.jar
    aspectjrt.jar
    aspectjweaver.jar
    ehcache-1.2.3.jar
    spring-aop.jar
    spring-aspects.jar
    spring-beans.jar
    spring-context.jar
    spring-core.jar
    spring-dao.jar
    spring-hibernate3.jar
    spring-jdbc.jar
    spring-mock.jar
    spring-support.jar
    spring-web.jar
    commons-logging.jar
    commons-collections-2.1.1.jar
    commons-attributes-api.jar
    cglib-nodep.jar

  6. #6
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    I had tried the code before I posted and it all worked. Please double check your code.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  7. #7
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    In the bug report (I assume you copy and pasted code), I don't see you followed the "Remove @Aspect from HelloRunner." step.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  8. #8
    Join Date
    Dec 2006
    Location
    Nottingham, UK
    Posts
    39

    Default

    Quote Originally Posted by ramnivas View Post
    In the bug report (I assume you copy and pasted code), I don't see you followed the "Remove @Aspect from HelloRunner." step.

    -Ramnivas
    I'm so sorry, I was following things to the letter, or so I thought, I must have misread, though how I don't know.

    Anyway, I've removed @Aspect from HelloRunner and now I do get the advice.

    But...

    Now we get to the Pointcut reference resolution problem. If I change from the in place pointcut to the reference pointcut I get:

    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myWorld' defined in file [D:\workspace\Timeout\build\WEB-INF\classes\applicationContext\test-aspects.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myPointcut
    Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myPointcut
    If you look back at the HelloAspect code you will see a commented out reference Pointcut. Using this produces the above error.

    Thanks again for your time on a Sunday!

  9. #9
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    Works for me... so there must be something else.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  10. #10
    Join Date
    Dec 2006
    Location
    Nottingham, UK
    Posts
    39

    Default

    Quote Originally Posted by ramnivas View Post
    Works for me... so there must be something else.

    -Ramnivas
    It is failing during the xml load

    I just have these beans declared in the xml, AnotherAspect delcares the PointCut and HelloAspect references it.
    Declaration order should not matter?

    Code:
    <bean id="helloRunner" class="timeout.helloworld.HelloRunner"></bean>
    	<bean id="anotherAspect" class="timeout.helloworld.AnotherAspect"></bean>
    	<bean id="helloAspect" class="timeout.helloworld.HelloAspect"></bean>
    	<bean id="myWorld" class="timeout.helloworld.worlds.MyWorld"></bean>

Posting Permissions

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