Announcement Announcement Module
Collapse
No announcement yet.
Interface or Abstract class as target in aop:aspect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Interface or Abstract class as target in aop:aspect

    Is it possible to use an Interface (or an Abstract class) in target for aop:aspect.

    org.ImyInterface is an Interface.

    If I write code as args(myArgument) where myArgument is the name of the single argument that update method accepts, it works.

    Instead of hard-wiring to the name of the argument that update method takes, I want to use data type (type of object that myArgument takes).

    myArgument will be of type ImyInterface. As such, instead of putting args(myArgument), can I write as target(org.ImyInterface)?


    <aop:config>
    <aop:aspect ref="myUtil">
    <aopointcut
    id="myId"
    expression="execution(* org.*.update*(..)) and target(org.ImyInterface)"
    />
    <aop:around
    method="aroundUpdate"
    pointcut-ref="myUpdateOperations"
    />
    </aop:aspect>
    </aop:config>

    With target(org.ImyInterface) I get following error:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'messageSource' defined in class path resource [spring-example.xml]: 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#1': 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(Access Controller.java:219)


    I do not have a concrete class that I can use in target that would be inherited by all the objects that will be passed to the update method. If I only have an Interface (and an Abstract class), but no concrete class, what do I need to do to put in expression, so that I do not hard-code the name of the argument?

  • #2
    You use 'target' pointcut at the wrong way. It is used to specify the object which method is being invoked, not invocation argument. You should use 'args' pointcut with the argument class for achieving your goal. I created corresponding blog article that contains complete example of such use-case.

    Comment


    • #3
      Can you use an interface or abstract class in args

      If I type the name of the argument (e.g. myArg) then I succeed.

      where method is update(org.IDomain myArgs)

      However, if I pass
      args(org.IDomain) it fails, where IDomain is an interface.

      Comment


      • #4
        Get exception when I pass Interface in args

        If I pass the name of the Interface (as opposed to the name of the variable) in args, I get exception.


        For method update(org.IDomain myVariable), args(myVariable) works.

        If I change XMl to args(org.IDomain), I get following exception:

        org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '_protectPointcutPostProcessor': 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#1': 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(Access Controller.java:219)
        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:221)
        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.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:881)
        at org.springframework.context.support.AbstractApplic ationContext.registerBeanPostProcessors(AbstractAp plicationContext.java:606)
        at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:366)
        at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
        at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:93)
        at org.nccourts.aoc.example.service.ConcurrencyTest.t estServiceUpdate(ConcurrencyTest.java:26)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:79)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:618)
        at junit.framework.TestCase.runTest(TestCase.java:154 )
        at junit.framework.TestCase.runBare(TestCase.java:127 )
        at junit.framework.TestResult$1.protect(TestResult.ja va:106)
        at junit.framework.TestResult.runProtected(TestResult .java:124)
        at junit.framework.TestResult.run(TestResult.java:109 )
        at junit.framework.TestCase.run(TestCase.java:118)
        at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
        at junit.framework.TestSuite.run(TestSuite.java:203)
        at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:130)
        at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)
        Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAd visor#1': 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(Access Controller.java:219)
        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:221)
        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)
        ... 31 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)
        ... 49 more
        Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
        at org.aspectj.weaver.tools.PointcutParser.parsePoint cutExpression(PointcutParser.java:316)
        at org.springframework.aop.aspectj.AspectJExpressionP ointcut.buildPointcutExpression(AspectJExpressionP ointcut.java:205)
        at org.springframework.aop.aspectj.AspectJExpressionP ointcut.checkReadyToMatch(AspectJExpressionPointcu t.java:192)
        at org.springframework.aop.aspectj.AspectJExpressionP ointcut.getMethodMatcher(AspectJExpressionPointcut .java:178)
        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:67)
        at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Construc tor.java:522)
        at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:100)
        ... 51 more

        However, the example in Spring documentation show that I can pass Serializable, which is an Interface.

        For testing purposes, I passed as args(java.lang.Object), has same exception as above.

        Comment


        • #5
          I'm afraid I don't quite understand your current concern.

          I worked out the complete standalone example that shows that it's possible to intercept only the calls where argument of specific type is used. If you have a problem with that you can create complete standalone test-case/modify the example I posted at the blog and post it here.

          Again, it's really unclear what problem you're experiencing at the moment.

          Comment

          Working...
          X