使用 Spring 和 AspectJ 拦截私有方法 [英] Intercept private methods with Spring and AspectJ
问题描述
我正在尝试使用 AspectJ 的加载时间编织和 Spring-Boot 和基于注释的配置在私有方法之前执行代码,我正在努力弄清楚为什么我的方面没有被调用.
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.
我的简单方面如下:
@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 中有以下内容:
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>
最后,根据文档,我还使用 -javaagent:/path/to/spring-instrument.jar
选项启动我的应用程序.我的类路径(spring-aop
和 aspectjweaver
)上也有所需的 jar,如 Spring 文档中所述.
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.
这甚至可能吗?AspectJ 文档中列出了一个限制",指出 注释样式不支持特权方面.
位于 这里,但是this Spring Forum 中较旧的帖子似乎表明这确实是可能的.
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.
推荐答案
我以为它会起作用,但我也以为您必须将 aspectjweaver.jar 添加为 javaagent.其中一个假设是错误的?
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屋!