Announcement Announcement Module
No announcement yet.
Bean Injection, Proxies, and Aspects Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean Injection, Proxies, and Aspects


    I'm sure this may be a very basic question, but unfortunately, the application I'm working on behaves different from my simple test case. This is with regards to AOP Proxy and and Aspects.

    Assume bean "test" is the target of an Aspect X.

    I find that when i do a ApplicationContext.getBean("test") and run some method, the Aspect fires correctly. However, once inject "test" into another bean as a setter dependency, the aspect no longer fires.

    I'm inserting <bean class="org.springframework.aop.aspectj.annotation. AnnotationAwareAspectJAutoProxyCreator" /> into my xml config.

    So my questions are:
    1) When using setter dependency injection, does it not create a Proxy - thus not allowing the Aspect to fire?
    2) I assume ApplicationContext.getBean() makes use of Proxy?
    3) Is there a scenarion where the above could potentially happen? Somehow where Applicationcontext.getBean() interferes with the dependency injection of a target bean of an aspect.

    thanks in advance

  • #2
    I think you may want to first explore the <aop: > namespace, since the direct use of AnnotationAwareAspectJAutoProxyCreator may not have been properly configured. As for the question, injecting a bean into another should have no effect on the application of an aspect (assuming you did it right).



    • #3
      I found out with that problem was. Sorry i wasn't more descriptive in my question, but i didn't want to inundate people with lines of code.

      The problem was that we were using GenericApplicationContext. In order to use aspects, i found that you needed to do a context refresh in order to activate them.

      Note: If you do a getBean before you do the refresh, any beans tied to it may not work with the aspect. In our case there was one "main" bean that was tied to tons of other things. So our code was something like:


      In this case, sometimes the aspects will work (but only after a bean retreived by "getBean", NOT injected), but not all the time. Behavior is bizarre.

      Flip the 2 statements around and it'll work perfect - injection and all.