Announcement Announcement Module
Collapse
No announcement yet.
How to enable @Autowired in aspects when using LTW? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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

    LogManagerAspect.java
    Code:
    @Aspect
    public class LogManagerAspect
    {
            private Log log = LogFactory.getLog(getClass());
    
            @Autowired
            ApplicationContext context;
    
            Selector selector;
    
            @Before("com.fundserv.simple.aspect.system.SystemArchitectureAspect.allOperations()")
            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

    ClassPathXmlApplicationContext
    <context:load-time-weaver />
    <context:annotation-config/>


    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

    Note

    <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.


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

    Comment


    • #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.

      Comment


      • #4
        Glad it is solved.

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

        Comment

        Working...
        X