JavaAspectJ:custom*。忽略aj文件
为什么aspectj-maven-plugin
忽略我的AnnotationInheritor.aj
文件?我做错什么了吗
我想用自定义注释为ItemRepository#getById
提供建议:
@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
// AOP does not work, since autogenerated ItemRepositoryImpl#getById
// won't have @MyAnnotation annotation
@MyAnnotation
public Item getById(Long id);
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation {
}
@Aspect
@Component
public class MyAspects {
@Around("@annotation(MyAnnotation)")
public Object execute(ProceedingJoinPoint joinPoint) {
// This advice works correct when @MyAnnotation is placed on class, I tested.
// The problem is that I have to put @MyAnnotation on interface method
}
}
Spring数据JPA使用接口和Java注释从不从接口继承到子类(由于JVM的限制)。让我的建议与自定义注释there is a little AspectJ trick配合使用。如前一篇参考文章所述,我创建了AnnotationInheritor.aj
文件:
package com.vbakh.somepackage.aspects;
// For some reason does not work. WHY?
public aspect AnnotationInheritor {
declare @method : void ItemRepository+.getById() : @MyAnnotation;
}
并将以下配置添加到我的pom.xml
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- IMPORTANT -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.9</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8 </encoding>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
另外,有没有办法在没有*的情况下执行相同的逻辑。aj文件?意思是带*。java文件
# 1 楼答案
我想即使没有
AnnotationInheritor
,它也应该能工作制作了一个小演示,看看
demo
# 2 楼答案
我将您的代码复制到AspectJ项目中(那里没有Spring或Spring AOP)以测试它。我发现了一些问题:
@Around("@annotation(MyAnnotation)")
将找不到注释,因为没有完全限定的类名declare @method : void ItemRepository+.getById() : @MyAnnotation;
与接口方法的签名Item getById(Long id)
不匹配MyAspects.execute(..)
需要抛出Throwable
,当然也会返回一些东西,比如joinPoint.proceed()
的结果。但也许这只是草率的复制;粘贴修复此问题后,以下MCVE运行良好:
使项目编译的助手类:
标记注释:
驱动程序应用程序:
方面:
如果你想知道为什么我要在切入点中添加
execution(* *(..))
,这是因为我想排除AspectJ中可用的匹配call()
连接点,而不是Spring AOP控制台日志:
瞧!它工作得很好
如果这样对你不起作用,你还有其他问题,比如(但不是唯一的)
你顺便提到的“自动生成的
ItemRepositoryImpl#getById
”。无论何时何地在构建过程中生成,在应用方面之前,它都需要存在。为了分析这一点,我需要在GitHub上安装一个MCVE将方面编织成的目标代码是否与方面代码位于同一个Maven模块中。如果不是,则需要更改Maven设置