Announcement Announcement Module
No announcement yet.
How to enable @Autowired in aspects when using LTW? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to enable @Autowired in aspects when using LTW?

    I have the following aspect that requires autowired of an application context
    public class LogManagerAspect
            private Log log = LogFactory.getLog(getClass());
            ApplicationContext context;
            Selector selector;
            public void beforeTraced(JoinPoint pjp) throws Exception
                    MethodSignature sig = (MethodSignature) pjp.getSignature();
                    StringBuffer buffer = new StringBuffer();
                    Selector selector = (Selector) context.getBean("defaultSelector");
                    if (sig != null && selector.isFiltered(sig))
                            buffer.append("Entering [" + sig.toShortString() + "]");
    It seems when using these

    <context:load-time-weaver />

    for weaving aspects into the code, @Autowired would not be detected in any aspect codes, but @Autowired would still works in any Controllers or anything annotated with @Configurable (as documented in the online reference). Is there a way to enable @Autowired in my aspect body?

    Here is the online reference


    <context:annotation-config/> only looks for annotations on beans in the same application context in which it is defined. This means that, if you put <context:annotation-config/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Autowired beans in your controllers, and not your services. See Section 15.2, “The DispatcherServlet” for more information.
    Last edited by zollen; Feb 14th, 2010, 06:55 PM. Reason: [SOLVED]

  • #2
    The strange thing is that @Autowired actually would works in my aspect body when using spring AOP (instead of load time weaver) + annotation based configuration.

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    Last edited by zollen; Feb 14th, 2010, 04:47 PM.


    • #3
      [SOLVED] I think I have answered my own question...

      For load time weaver to weave any non-bean/non-controller objects. The annotation @Configurable must be declared for the target class.

      I declare the @Configurable on my aspect and now ApplicationContext is available.


      • #4
        Glad it is solved.

        If you wanted to not mark the aspect as @Configurable, you could have done the following:
        <bean class="yourpackge.LogManagerAspect" factory-method="aspectOf"/>