使用 Aspectj 加载时间编织来记录我的源代码(包括测试)而不记录外部库

Use Aspectj load time weaving for logging my source (include test) without logging external libraries

我仅在测试执行时使用 AspectJ 进行日志记录,因此我使用加载时织入。我将 Interceptor 打包到一个 jar 文件中,以便与另一个 Maven 项目一起使用。但是使用下面的配置,aspectjweaver 将编织外部库的方法。我希望它只编织我的源代码(包括测试)而没有像 <include within="hello.*"/> 这样的特定配置,以便通用使用像依赖项。

对不起,我的英文很烂。非常感谢!!!

在这个jar文件的aop.xml中,它喜欢

<aspectj>
<aspects>
    <aspect name="log.Interceptor"/>
    <weaver options="-verbose -showWeaveInfo">
        <include within="*"/>
    </weaver>
</aspects>
</aspectj>

// 拦截器

pointcut traceMethods() : (execution(* *(..)) && !cflow(within(Interceptor)) && !within(*Test) && !within(Test*) && !within(*Tests) && !within(*TestCase));
before(): traceMethods(){
    Method method = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod();
    logDebug(method, LogPattern.METHOD_START);
}
after(): traceMethods(){
    Method method = ((MethodSignature) thisJoinPointStaticPart.getSignature()).getMethod();
    logDebug(method, LogPattern.METHOD_FINISH);
}` 

嗯,你不能一边吃蛋糕一边留着蛋糕。因此,您的解决方案要么是针对整个世界的切入点的通用解决方案,要么是通过包含 and/or 排除。

  • 您可以尝试在使用通用库构建或部署项目时在本地使用额外的 META-INF/aop.xml 并在其中指定 in-/excludes。据我所知,AspectJ 会在类路径中找到所有这些文件并合并在那里找到的设置。

  • 或者,您可以定义一个抽象切入点,并告诉您的用户在他们项目的 aop.xml 文件中将其具体化。

这两个建议的解决方案都应该有效,但它们需要您的库的用户对 AspectJ 或至少切入点语法有基本的了解。要么你这样做并用示例切入点或整个示例 AOP 配置文件相应地记录你的库,要么你必须在你自己的切入点中更具体 and/or in-/excludes。没有神奇的解决方案,AspectJ 编织器无法猜测您想要编织什么,什么不是。