Search code examples
springaopaspectjspring-aop

spring @Aspect not injecting dependencies


I am using compile time weaving using maven, spring and aspectj

my aspectj advisor looks like this

@Aspect
public class LoggingInterceptor {
  private LogManager logManager;
  public void setLogManager(LogManager logManager) {
    this.logManager = logManager;
  }
  .....
} 

My applicationContext.xml look like this

<!--configures the AspectJ aspect and indicates which Spring context should be used when giving advice-->
<context:spring-configured />

<aop:aspectj-autoproxy/>

<!--<context:component-scan base-package="com.reverb" />-->

<bean id="loggingInterceptor" class="com.myapp.interceptor.LoggingInterceptor">
    <property name="logManager" ref="logManager" />
</bean>

The logManager is always null....


Solution

  • I don't see your logManager to be defined anywhere. Even if it is, @Aspects are not automatically eligible for injection. In fact what happens is that you have 2 objects - one is a bean of type LoggingInterceptor, and the other is the aspect, which actually handles the AOP. But the aspect is not a bean.

    In order to make this work, you'd need to define factory-method="aspectOf" for your <bean>. See here for more info.