我试图在IntelliJ IDEA的示例项目中使用AspectJ.我有使用
Spring AOP的经验,但这是我第一次使用AspectJ,并且无法使其工作.
我想按照这里描述的方式去做:https://www.jetbrains.com/help/idea/2017.1/aspectj.html
我的build.gradle:
apply plugin: 'java' repositories { mavenCentral() } dependencies { compile "org.projectlombok:lombok:+" compile "org.aspectj:aspectjrt:+" compile "org.aspectj:aspectjweaver:+" compile "org.aspectj:aspectjtools:+" } buildscript { repositories { maven { url "https://maven.eveoh.nl/content/repositories/releases" } } dependencies { classpath "nl.eveoh:gradle-aspectj:+" } } project.ext { aspectjVersion = '+' } apply plugin: 'aspectj'
我的方面:
package aspects; import lombok.SneakyThrows; import lombok.experimental.FieldDefaults; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import java.util.Arrays; @Aspect @FieldDefaults(makeFinal = true) public aspect LoggingAspect { Journal journal = Journal.INSTANCE; pointcut all(ProceedingJoinPoint proceedingJoinPoint) : execution(* * set*(..) ); around() : all(ProceedingJoinPoint proceedingJoinPoint) { System.out.println("asd"); } @SneakyThrows @Around("execution(* * repository.*.*(..))") public Object log(ProceedingJoinPoint proceedingJoinPoint) { journal.write(proceedingJoinPoint.getThis().getClass().getCanonicalName()); journal.write("\n"); String arguments = Arrays .stream(proceedingJoinPoint.getArgs()) .map(o -> o.getClass().getCanonicalName() + " " + o.toString()) .reduce(new StringBuilder(),StringBuilder::append,StringBuilder::append) .toString(); journal.write(arguments); journal.write("\n"); long start = System.currentTimeMillis(); Object result = proceedingJoinPoint.proceed(); journal.write(result.toString()); journal.write("\n"); journal.write(String.valueOf(System.currentTimeMillis() - start)); journal.write("\n\n"); journal.flush(); return result; } }
样本类:
package repository; import java.io.Serializable; public class Task implements Serializable { private static final long serialVersionUID = 1L; enum Status {NEW,IN_PROGRESS,FINISHED}; private Integer id; private String description; private Employee assignee; private Employee reporter; private Status status; public Task(final Integer id,String description,final Employee assignee,final Employee reporter) { this.id = id; this.assignee = assignee; this.reporter = reporter; this.description = description; this.status = Status.NEW; } public Employee getAssignee() { return assignee; } public void setAssignee(Employee assignee) { this.assignee = assignee; } ... }
我有IntelliJ IDEA Ultimate 2017.2,它正确地暗示所有方法(吸气剂和制定者)都是我的减产.它还暗示我,对于我的方面的“日志”建议,有多个减少点.但是,它没有暗示“所有”建议.
我安装了(在最新版本中)插件,如:
– AspectJ支持
– AspectJ weaver
– Spring AOP / @ AspectJ
Gradle依赖项自动为此项目创建库,因此我没有复制它.
我启用了:Build> AspectJ编织.
设置>构建,执行,部署>编译器> Java编译器:
使用编译器:ajc
Ajc编译路径:/home/me/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjtools/1.8.10/5c5957ee4615bde33b54ce965f5b85b8827b97d5/aspectjtools-1.8.10.jar
命令行参数:为空
生成调试信息:已选中
委托给javac:chcecked
启用注释处理选项:选中
项目构建和编译没有问题.它运行就像从来没有任何方面,特殊性没有从方面打印出来,也没有创建临时文件,并且所有方法运行良好(尽管在我的“所有”建议中,我没有处理关节点).
当我在任何建议的第一行创建断点并使用调试器运行项目时,不会捕获任何内容.
我错过了什么?