Announcement Announcement Module
Collapse
No announcement yet.
PointCut Expression with Custom Annotation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PointCut Expression with Custom Annotation

    Hi,

    I am trying to do AOP on Method which has custom Annotation

    E.g
    The Method Signature is as below.


    @MyPermission(values=MyPermission.CREATE)
    public String printHello(String data){
    System.out.println("#$# Print Data "+data);
    return "data "+" Invoked";
    }

    and I am trying to write Spring AOP using expression pointcut which get invoke before printHello.

    ..... XML AOP
    <aop:aspect id="MyAspect" ref="myAOPHandler">
    <aop:before method="printData"
    pointcut="execution(java.lang.String com.test.practise..*Service.*(java.lang.String,..) ) and args(data,..) and @annotation(com.test.practise.annotations.MyPermis sion)" />
    </aop:aspect>

    I am receiving error as below while trying to deploy war application

    Error Message:
    BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAd visor#3': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdv isor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
    at
    org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:405)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:380)
    at org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 45)
    at weblogic.servlet.internal.EventsManager$FireContex tListenerAction.run(EventsManager.java:376)
    at weblogic.security.acl.internal.AuthenticatedSubjec t.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(Se curityManager.java:121)
    ....
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAd visor#3': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdv isor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:254)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:925)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:835)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:440)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.aop.framework.autoproxy.BeanFa ctoryAdvisorRetrievalHelper.findAdvisorBeans(BeanF actoryAdvisorRetrievalHelper.java:87)
    at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.findCandidateAdvisors(Ab stractAdvisorAutoProxyCreator.java:98)
    at org.springframework.aop.aspectj.autoproxy.AspectJA wareAdvisorAutoProxyCreator.shouldSkip(AspectJAwar eAdvisorAutoProxyCreator.java:105)
    at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.postProcessBeforeInstantiation( AbstractAutoProxyCreator.java:281)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyBeanPostProcessors BeforeInstantiation(AbstractAutowireCapableBeanFac tory.java:791)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.resolveBeforeInstantiat ion(AbstractAutowireCapableBeanFactory.java:762)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:399)
    ... 48 more
    Caused by: org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdv isor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:115)
    at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:87)
    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:248)
    ... 66 more
    Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
    at org.aspectj.weaver.tools.PointcutParser.parsePoint cutExpression(PointcutParser.java:317)
    at org.springframework.aop.aspectj.AspectJExpressionP ointcut.buildPointcutExpression(AspectJExpressionP ointcut.java:206)
    at org.springframework.aop.aspectj.AspectJExpressionP ointcut.checkReadyToMatch(AspectJExpressionPointcu t.java:193)
    at org.springframework.aop.aspectj.AspectJExpressionP ointcut.getMethodMatcher(AspectJExpressionPointcut .java:179)
    at org.springframework.aop.aspectj.AbstractAspectJAdv ice.buildSafePointcut(AbstractAspectJAdvice.java:1 89)
    at org.springframework.aop.aspectj.AspectJPointcutAdv isor.<init>(AspectJPointcutAdvisor.java:51)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:494)
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:100)
    ... 68 more


    Please help me on this..

  • #2
    Here's a sample method annotation for your reference.

    Declaring the annotation:
    Code:
    package com.foo;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(value = RetentionPolicy.RUNTIME)
    @Target(value = ElementType.METHOD)
    public @interface FooAnnotation {
    }
    Declaring the Aspect:
    Code:
    package com.foo;
    
    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 FooAnnotationAspect {
    
    	@Pointcut("@annotation(com.foo.FooAnnotation)")
    	public void function() {
    	}
    
    	@Before("@annotation(com.foo.FooAnnotation)")
    	public void foo(JoinPoint joinPoint) {
    	}
    }
    Declaring the Spring configuration:
    Code:
      <beans ...>
        <aop:aspectj-autoproxy />
        
        <bean class="com.foo.FooAnnotationAspect" />
      </beans>

    Comment


    • #3
      Hi,

      Thanks for the reply, but this is not much helpfull.

      I am looking to have method Arguments & it's Annotation passed to Advise

      Comment


      • #4
        Custom annotation property passed as a paramether to aop aspect

        I had the same problem and I solved it like this:

        /*the custom annotation*/
        package test.aop.annotations;

        @Retention(RetentionPolicy.RUNTIME)
        @Target(ElementType.METHOD)
        public @interface UCase {
        byte identifier();
        }

        /*method with the custom annotation*/
        @UCase(identifier = Operation.OPERATION_ADDITEM)
        public void someMethod(){}

        /*the aspect*/
        @Aspect
        public class MyAspect {
        @Around( value = "@annotation( ucase )" )
        public Object cache( ProceedingJoinPoint call, UCase ucase )
        throws Throwable {
        Object result = null;
        Method method = this.getCallMethod(call);
        System.out.println("identifier " + ucase.identifier());
        result = call.proceed();
        return result;
        }
        }

        This page helped me, so maybe you can find something else useful: http://ssc-coding.blogspot.com/2009/...ed-method.html

        Comment


        • #5
          U can also use Before / After method Advise

          Hi,

          I try to get a pass solution by placing before Advise and created a pointcut on method.

          Put we need something by Spring to detect annotation and method using expression.

          Comment


          • #6
            Concrete pointcuts defined in aop.xml cannot bind context. This is a restriction documented here: http://www.eclipse.org/aspectj/doc/r...oncrete-aspect

            You can write the pointcut to select the method of interest and then access the state through the JoinPoint object in the advice. So your pointcut might read:

            Code:
            execution(@com.test.practise.annotations.MyPermission java.lang.String com.test.practise..*Service.*(java.lang.String,..) )" />
            and in the advice the use of getArgs()[0] to get the first parameter and getSignature().getMethod().getAnnotations() (something like that) would give you access to the annotation.

            Andy Clement
            AspectJ Lead Committer
            SpringSource

            Comment

            Working...
            X