Announcement Announcement Module
Collapse
No announcement yet.
Can't get advice in the simplest example to execute Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


    • #3
      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 [[email protected]f6]
      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, 07:11 AM. Reason: Apending

      Comment


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

        Comment


        • #5
          JIRA placed

          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

          Comment


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

            -Ramnivas

            Comment


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

              -Ramnivas

              Comment


              • #8
                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!

                Comment


                • #9
                  Works for me... so there must be something else.

                  -Ramnivas

                  Comment


                  • #10
                    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>

                    Comment


                    • #11
                      No, the order should not matter. Just to verify I tried the same order as yours and I got:

                      Code:
                      DefaultWorld does something!
                      AnotherAspect.testAdvice()
                      HelloAspect.testAdvice()
                      HelloRunner.testAdvice()
                      Here is my advice
                      So it still works for me...

                      The only thing I can suggest is to create a minimal program that reproduces the problem. Also, you may want to try 2.0.1 (however, for me both 2.0final and 2.0.1 work just fine).

                      -Ramnivas

                      Comment


                      • #12
                        Originally posted by ramnivas View Post

                        The only thing I can suggest is to create a minimal program that reproduces the problem. Also, you may want to try 2.0.1 (however, for me both 2.0final and 2.0.1 work just fine).

                        -Ramnivas
                        Well I can't get much more minimal. But since the Introductions are working, I've removed them and just have 3 beans (and just those 3 delcared in xml)

                        AnotherAspect declares the Pointcut
                        HelloAspect references the Pointcut in AnotherAspect
                        And I run these from HelloRunner

                        Still the same error

                        To be honest it won't affect me much I won't be making extensive use of Aspects, I just want to rewrite an old AopAlliance DAO inplementation because that is causing conflicts with my new Transaction Aspects. So I just use the in place approach.
                        But I have the stamina to resolve this, if you have

                        Thanks

                        Comment


                        • #13
                          But I have the stamina to resolve this, if you have
                          I guess I have shown my stamina quite a bit already :-) Hey, I could even point out the changes you thought you made, but didn't!

                          At this point, there is not much I can do since I can't reproduce the problem you have.

                          -Ramnivas

                          Comment


                          • #14
                            All right... man, this thread is becoming my obsession :-)

                            I spot one critical difference between your ENVIRONMENT and mine -- you seem to be using Java 6 (extracted from your bug report) and I am using Java 5. I have seen such a bug with Java 6 (although haven't fully explored it). Can you try with Java 5 and confirm my hypothesis?

                            -Ramnivas

                            Comment


                            • #15
                              Originally posted by ramnivas View Post
                              All right... man, this thread is becoming my obsession :-)

                              I spot one critical difference between your ENVIRONMENT and mine -- you seem to be using Java 6 (extracted from your bug report) and I am using Java 5. I have seen such a bug with Java 6 (although haven't fully explored it). Can you try with Java 5 and confirm my hypothesis?

                              -Ramnivas
                              You are absolutely correct.
                              The final release of JDK6 released only last week, fails whilst JDK5 works fine.

                              Comment

                              Working...
                              X