我正在尝试使用AspectJ在IntelliJ IDEA中的示例项目中。我有使用Spring的经验,但这是我第一次使用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终极2017.2,它正确地暗示了所有方法(getter和setter)都是我的切入点。它还暗示我,在我的方面,对于“日志”有多个切点。然而,它并没有暗示“所有”建议。
我安装了(最新版本)插件,如:- AspectJ Support - AspectJ weaver - Spring /@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
启用注释处理选项:选中
项目构建和编译没有问题。它的运行就像从来没有任何方面,特性没有从方面打印出来,没有临时文件被创建,所有的方法运行良好(虽然在我的“所有”的建议周围,我还没有处理连接点)。
当我在任何建议的第一行中创建断点并使用调试器运行项目时,不会捕获任何内容。
我遗漏了什么?
发布于 2017-05-25 18:13:40
在浪费了更多的时间之后,我发现我将两个不同的AspectJ sytnax混合在一起。
一个是关于方面的(比如"public方面LoggingAspect"),第二个是常规的Java注解(比如“@方面”或“@ are”)。
当我将"public方面LoggingAspect“替换为"public class LoggingAspect”时,它起了作用。这是很难找到的,因为一切仍然没有问题的编译。
希望有一天它能帮助到某些人,或者至少在将来的ajc版本中,他们会添加更详细的编译器输出。
https://stackoverflow.com/questions/44088421
复制相似问题