Announcement Announcement Module
Collapse
No announcement yet.
ApplicationContext load fails once I make my aspect implement Ordered Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ApplicationContext load fails once I make my aspect implement Ordered

    Hello,

    I have a very complicated error, for which Google provided no help, so I'm hoping Spring gurus here can give me some tips.

    I have a Spring AOP aspect whose purpose of being is translating DB exceptions into our application's own exception.

    I have a JUnit for this aspect which configures a minimal spring application context, using @Configuration classes and the @EnableAspectJAutoProxy annotation.
    This is not the first JUnit for an aspect that I write. I have another one for an access control aspect that works just fine.

    But this particular aspect must implement the org.springframework.core.Ordered interface, so that I can assure that it will execute after Spring's transactional code (otherwise it will never catch any database exception).
    And as soon as I add such interface implementation to my aspect class, the once working JUnit breaks. The application context fails to load with this exception:

    Caused by: org.springframework.beans.factory.BeanCurrentlyInC reationException: Error creating bean with name 'aspect': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.beforeSingletonCreation(Defau ltSingletonBeanRegistry.java:300)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:219)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.aop.aspectj.annotation.BeanFac toryAspectInstanceFactory.getOrder(BeanFactoryAspe ctInstanceFactory.java:106)
    at org.springframework.aop.aspectj.annotation.LazySin gletonAspectInstanceFactoryDecorator.getOrder(Lazy SingletonAspectInstanceFactoryDecorator.java:69)
    at org.springframework.aop.aspectj.annotation.Instant iationModelAwarePointcutAdvisorImpl.getOrder(Insta ntiationModelAwarePointcutAdvisorImpl.java:153)
    at org.springframework.core.OrderComparator.getOrder( OrderComparator.java:70)
    at org.springframework.core.OrderComparator.compare(O rderComparator.java:57)
    at org.springframework.aop.aspectj.autoproxy.AspectJP recedenceComparator.compare(AspectJPrecedenceCompa rator.java:97)
    at org.springframework.aop.aspectj.autoproxy.AspectJA wareAdvisorAutoProxyCreator$PartiallyComparableAdv isorHolder.compareTo(AspectJAwareAdvisorAutoProxyC reator.java:135)
    at org.aspectj.util.PartialOrder$SortObject.addDirect edLinks(PartialOrder.java:71)
    at org.aspectj.util.PartialOrder.addNewPartialCompara ble(PartialOrder.java:93)
    at org.aspectj.util.PartialOrder.sort(PartialOrder.ja va:129)
    at org.springframework.aop.aspectj.autoproxy.AspectJA wareAdvisorAutoProxyCreator.sortAdvisors(AspectJAw areAdvisorAutoProxyCreator.java:79)
    at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.findEligibleAdvisors(Abs tractAdvisorAutoProxyCreator.java:90)
    at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.getAdvicesAndAdvisorsFor Bean(AbstractAdvisorAutoProxyCreator.java:68)
    at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.wrapIfNecessary(AbstractAutoPro xyCreator.java:359)
    at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.getEarlyBeanReference(AbstractA utoProxyCreator.java:268)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.getEarlyBeanReference(A bstractAutowireCapableBeanFactory.java:727)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$2.getObject(AbstractAut owireCapableBeanFactory.java:509)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:188)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:169)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:237 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.Construc torResolver.instantiateUsingFactoryMethod(Construc torResolver.java:353)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateUsingFactory Method(AbstractAutowireCapableBeanFactory.java:101 5)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:911)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.aop.aspectj.annotation.BeanFac toryAspectInstanceFactory.getOrder(BeanFactoryAspe ctInstanceFactory.java:106)
    at org.springframework.aop.aspectj.annotation.LazySin gletonAspectInstanceFactoryDecorator.getOrder(Lazy SingletonAspectInstanceFactoryDecorator.java:69)
    at org.springframework.aop.aspectj.annotation.Instant iationModelAwarePointcutAdvisorImpl.getOrder(Insta ntiationModelAwarePointcutAdvisorImpl.java:153)
    at org.springframework.core.OrderComparator.getOrder( OrderComparator.java:70)
    at org.springframework.core.OrderComparator.compare(O rderComparator.java:57)
    at org.springframework.aop.aspectj.autoproxy.AspectJP recedenceComparator.compare(AspectJPrecedenceCompa rator.java:97)
    at org.springframework.aop.aspectj.autoproxy.AspectJA wareAdvisorAutoProxyCreator$PartiallyComparableAdv isorHolder.compareTo(AspectJAwareAdvisorAutoProxyC reator.java:135)
    at org.aspectj.util.PartialOrder$SortObject.addDirect edLinks(PartialOrder.java:71)
    at org.aspectj.util.PartialOrder.addNewPartialCompara ble(PartialOrder.java:93)
    at org.aspectj.util.PartialOrder.sort(PartialOrder.ja va:129)
    at org.springframework.aop.aspectj.autoproxy.AspectJA wareAdvisorAutoProxyCreator.sortAdvisors(AspectJAw areAdvisorAutoProxyCreator.java:79)
    at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.findEligibleAdvisors(Abs tractAdvisorAutoProxyCreator.java:90)
    at org.springframework.aop.framework.autoproxy.Abstra ctAdvisorAutoProxyCreator.getAdvicesAndAdvisorsFor Bean(AbstractAdvisorAutoProxyCreator.java:68)
    at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.wrapIfNecessary(AbstractAutoPro xyCreator.java:359)
    at org.springframework.aop.framework.autoproxy.Abstra ctAutoProxyCreator.postProcessAfterInitialization( AbstractAutoProxyCreator.java:322)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyBeanPostProcessors AfterInitialization(AbstractAutowireCapableBeanFac tory.java:407)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1461)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
    ... 37 more


    Removing the implements clause and the @Override annotation from the getOrder method solves the problem: my test passes again.

    What's going on? Why adding implements Ordered creates a circular reference? My method getOrder is as simple as this:

    Code:
        @Override
        public int getOrder() {
    	return ExposeInvocationInterceptor.INSTANCE.getOrder() + 1;
        }
    Does anyone have any idea on what's going on?

    Thank you very much.
    Last edited by caveden; Mar 22nd, 2013, 09:05 AM. Reason: Adding code quotes

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable...

    Removing the implements clause and the @Override annotation from the getOrder method solves the problem: my test passes again.
    Which is basically the aspect without any Ordered interface...

    Instead of the interface try @Order (the spring one) and see if that works.

    Implementing the interface could make your aspect eligeble for proxy creation, proxy creation requires aspects to be available which could lead to this exception.

    Comment


    • #3
      Ha!
      Using the annotation instead of the interface works (of course I had to replace the returned value by a constant expression, what now forces me to create a test to keep track of ExposeInvocationInterceptor.INSTANCE.getOrder(), but that's fine).

      Thank you very much Marten.

      Isn't this a bug in Spring though? I have a hard time understanding what I was doing wrong.

      Comment


      • #4
        No it isn't a Spring bug...

        It is a, well-educated but still, guess. The introduction of the interface lead to creation of a proxy around your aspect (probably due to some of your other aspects, configuration). The proxy creations adds the interceptors/aspects to the proxy... Which in this case would lead to a circular dependency.

        AspectJ Autoproxy can lead to proxy creation of all beans, depending on your pointcut expression. What a lot of people don't realize a pointcut expression can contain static and dynamic evaluated parts. The static parts can be evaluated on startup, the dynamic part only at runtime and for that spring needs to proxy all beans. And no that isn't a spring bug this is also what would happen when using AspectJ with Loadimte and/or compile time weaving (the aspect applied to a lot of beans).

        Comment


        • #5
          Does it makes sense for an aspect bean to intercept itself?

          EDIT: And you're right about the fact that it's linked to the way things are configured. The actual application is configured via XMLs, and it worked just fine, no circular references. My tests, on the other hand, are configured via @Configurable classes embedded in the test itself. Only the tests failed.

          Comment


          • #6

            Does it makes sense for an aspect bean to intercept itself?
            A bean is a bean the role it plays isn't important...

            Comment

            Working...
            X