使用Spring和AspectJ拦截私有方法 [英] Intercept private methods with Spring and AspectJ
问题描述
我试图在使用私有方法使用AspectJ的加载时编织和基于Spring-Boot以及基于注释的配置之前执行代码,并且试图找出为什么未调用我的方面的原因./p>
我的简单方面如下:
@Aspect
public class LoggingAspect {
private static Logger log = LoggerFactory.getLogger(LoggingAspect.class);
@Before("execution(private * com.mycompany.MyServiceWithPrivateMethods.*(..))")
public void privateAspect() {
log.warn("#### Private method aspect invoked!");
}
}
我的Spring配置类上还有@EnableLoadTimeWeaving
批注,在META-INF/aop.xml中也有以下批注:
<aspectj>
<weaver options="-verbose">
<!-- only weave classes in our application-specific packages -->
<include within="com.mycompany.*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.mycompany.LoggingAspect"/>
</aspects>
</aspectj>
最后,根据文档,我还使用-javaagent:/path/to/spring-instrument.jar
选项启动了我的应用程序.我在类路径(spring-aop
和aspectjweaver
)上也有必需的jar,如Spring文档中所述.
这甚至有可能吗?在AspectJ文档中列出了一个限制",指出位于的Privileged aspects are not supported by the annotation style.
,但是此较早的帖子似乎表明确实有可能.
我本以为可以,但是我也以为您必须将Aspectjweaver.jar添加为javaagent.这些假设之一是错误的?
I am trying to execute code before a private method using AspectJ's load-time-weaving with Spring-Boot and annotation-based configuration and I'm pulling my hair out trying to figure out why my aspect is not being invoked.
My simple aspect is as follows:
@Aspect
public class LoggingAspect {
private static Logger log = LoggerFactory.getLogger(LoggingAspect.class);
@Before("execution(private * com.mycompany.MyServiceWithPrivateMethods.*(..))")
public void privateAspect() {
log.warn("#### Private method aspect invoked!");
}
}
I also have the @EnableLoadTimeWeaving
annotation on my Spring configuration class and the following in my META-INF/aop.xml:
<aspectj>
<weaver options="-verbose">
<!-- only weave classes in our application-specific packages -->
<include within="com.mycompany.*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.mycompany.LoggingAspect"/>
</aspects>
</aspectj>
Lastly, I am also starting my application with the -javaagent:/path/to/spring-instrument.jar
option, per the docs. I also have the required jars on my classpath (spring-aop
and aspectjweaver
) as also mentioned in the Spring docs.
Is this even possible? There is a 'limitation' listed in the AspectJ docs that states Privileged aspects are not supported by the annotation style.
located here, however this older post in the Spring Forum seems to indicate that it is indeed possible.
I would have thought it would work, but I would have also thought you would have to add aspectjweaver.jar as a javaagent. One of those assumptions is wrong?
这篇关于使用Spring和AspectJ拦截私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!