Announcement Announcement Module
Collapse
No announcement yet.
AOP Annotation based point cuts Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP Annotation based point cuts

    Hi all,
    I have decided that I would like to implement an annotation based point cut.
    That is that when a method is annotated with my "pointCutAnnotation" around advice would be applied to that method.

    I am using Eclipse and Sping IDE to ensure the point cuts are activated, using the bean cross reference view.

    However, it isint work. It isint showing my the advises information in the view, i know the view works as simple bean() point cuts work right.

    I have the AOP autoproxy tag in my context file, i have created teh required annotation. It just dosnt seem to be working at all.

    Is it possible to advise simple on a method with an annotation?
    Can i advise private and protected methods, if not why do we have the modifier in the point cut expressions?

    Thanks
    Gavin

  • #2
    Originally posted by Encrypt View Post
    Is it possible to advise simple on a method with an annotation?
    Yes, use @annotation pointcut designator.



    Originally posted by Encrypt View Post
    Can i advise private and protected methods, if not why do we have the modifier in the point cut expressions?
    6.2.3.1. Supported Pointcut Designators:
    Due to the proxy-based nature of Spring's AOP framework, protected methods are by definition not intercepted, neither for JDK proxies (where this isn't applicable) nor for CGLIB proxies (where this is technically possible but not recommendable for AOP purposes). As a consequence, any given pointcut will be matched against public methods only!
    The reason for modifier presence at pointcut expression is that spring @aspectj aop uses aspectj pointcut expression library and aspectj allows to advice non-public methods.

    Generally speaking you said that you tried to use aspect with annotation-based pointcut and didn't succeed. Well, as no other information is provided I can only say that that's sad. Build small standalone test-case that shows the problem and post it here.

    Comment


    • #3
      Thank you for the reply.
      I didnt post more information as i wasnt sure what i trying to doing was even possible as i was having so much hassel with it.

      But I think your response has given me an insight into what I am doing wrongly.

      Comment


      • #4
        More detail!!!

        Ok, so i finally got the @annotation point cut to work!
        Well that is the Spring IDE bean cross reference dispaly is saying that the advice will be applied.

        However, I am seeing slightly odd behaviour, mostly in that is a class implement an interface, it seems that the annotation only works against methods that are overriding those from the interface.

        For example i have a class that implements Runnable, I can see my advice (again in teh cross reference view) being applied to the run method, but not to the three other methods in the class, a getter and a setter and the method being used to kick off the thread.

        However if the class has no interface to implement, it seems all the public classes can have advice applied to them.

        Could this be an artifact of the Spring IDE rather? I hope not as I am about to advice the rest of my team as to how to use it.
        (Will write back with result of testing the AOP stuff running)

        Comment


        • #5
          Ok, it dosnt work in test
          I get a bean creation exception (see end of post)
          I basicly have a very simple annotation, with the Rentention Policy of Runtime.
          Then i have an after Aspect, that has a point cut defiend to work on the Annotation.

          As noted before, the method I have wrapped is defined in an interface, and is public.

          dont think i have missed anything, thanks, heres the exception:

          Code:
          org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'keyRotation' defined in class path resource [keyRotationContext.xml]: Cannot resolve reference to bean 'keyRotationDelegate' while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'keyRotationDelegate' defined in class path resource [keyRotationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.virginmobile.keyrotation.service.BatchKeyRotationControl]: Could not convert constructor argument value of type [$Proxy6] to required type [com.virginmobile.keyrotation.service.BatchKeyRotationControl]: Failed to convert value of type [$Proxy6] to required type [com.virginmobile.keyrotation.service.BatchKeyRotationControl]; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy6] to required type [com.virginmobile.keyrotation.service.BatchKeyRotationControl]: no matching editors or conversion strategy found
          	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
          	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
          	at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:464)
          	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:156)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:855)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:765)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:412)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:383)
          	at java.security.AccessController.doPrivileged(Native Method)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:353)
          	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
          	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
          	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
          	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
          	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:400)
          	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:736)
          	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
          	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:123)
          	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
          	at com.virginmobile.SpringApplicationStarter.main(SpringApplicationStarter.java:32)
          Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'keyRotationDelegate' defined in class path resource [keyRotationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.virginmobile.keyrotation.service.BatchKeyRotationControl]: Could not convert constructor argument value of type [$Proxy6] to required type [com.virginmobile.keyrotation.service.BatchKeyRotationControl]: Failed to convert value of type [$Proxy6] to required type [com.virginmobile.keyrotation.service.BatchKeyRotationControl]; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy6] to required type [com.virginmobile.keyrotation.service.BatchKeyRotationControl]: no matching editors or conversion strategy found
          	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:534)
          	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:186)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:855)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:765)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:412)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:383)
          	at java.security.AccessController.doPrivileged(Native Method)
          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:353)
          	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
          	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:169)
          	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
          	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
          	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
          	... 19 more

          Comment


          • #6
            Your both problems (only overridden annotations and the type conversion error) may be solved by adding proxy-target-class="true" in <aop:aspect-autoproxy>.

            -Ramnivas

            Comment


            • #7
              Thanks, that seems to have given me the result i want.
              However I am confused, as I have Advice that is defined based on pointcuts that are "bean" rather then annotation, and the bean ones work fine.

              More reading for me I guess. Thank you all for your help

              G

              Comment

              Working...
              X