spring @Aspect 不注入依赖项
我正在使用 Maven、Spring 和 AspectJ 进行编译时编织
我的 AspectJ Advisor 看起来像这样
@Aspect
public class LoggingInterceptor {
private LogManager logManager;
public void setLogManager(LogManager logManager) {
this.logManager = logManager;
}
.....
}
我的 applicationContext.xml 看起来像这样
<!--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>
LogManager 始终为空......
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有看到您的
logManager
在任何地方定义。即使是,@Aspect
也不会自动符合注入条件。事实上,您有 2 个对象 - 一个是 LoggingInterceptor 类型的 bean,另一个是实际处理 AOP 的切面。但方面不是豆。为了使其工作,您需要为
定义factory-method="aspectOf"
。 参见此处< /a> 了解更多信息。I don't see your
logManager
to be defined anywhere. Even if it is,@Aspect
s are not automatically eligible for injection. In fact what happens is that you have 2 objects - one is a bean of typeLoggingInterceptor
, 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.使用 java 配置,它看起来像:
不要忘记:
@Configurable
注释在方面
Add
'org.springframework:spring-aspects'
as编译依赖添加
META-INF/aop.xml
内容:
-javaagent:/path-to-aspectj/aspectjweaver-1.8.10.jar
启用 javaagentWith java configuration it will be look like:
Don't forget to:
@Configurable
annotationon aspect:
Add
'org.springframework:spring-aspects'
as compile dependencyAdd
META-INF/aop.xml
with content:
-javaagent:/path-to-aspectj/aspectjweaver-1.8.10.jar