IntelliJ IDEA + AspectJ [英] IntelliJ IDEA + AspectJ

查看:51
本文介绍了IntelliJ IDEA + AspectJ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 IntelliJ IDEA 的示例项目中使用 AspectJ.我有使用 Spring AOP 的经验,但这是我第一次使用 AspectJ,无法使其工作.

I am trying to use AspectJ in sample project in IntelliJ IDEA. I have an experience with Spring AOP, but this is first time I am using AspectJ, and cannot make it work.

我正在尝试按照此处所述进行操作:https://www.jetbrains.com/help/idea/2017.1/aspectj.html

I am trying to do as described here: https://www.jetbrains.com/help/idea/2017.1/aspectj.html

我的 build.gradle:

My 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,它正确提示所有方法(getter 和 setter)都是我的切入点.它还暗示我,在我的方面,建议记录"有多个切入点.但是,它并没有暗示所有"建议.

I have IntelliJ IDEA Ultimate 2017.2 and it hints correctly that all methods (getters and setters) are my point-cuts. It also hints me that for advice "log" in my aspect there are multiple point-cuts. However, it does not hint about "all" advice.

我已经安装了(在最新版本中)插件,例如:- AspectJ 支持- AspectJ 织布工- Spring AOP/@AspectJ

I have installed (in newest version) plugins like: - AspectJ Support - AspectJ weaver - Spring AOP/@AspectJ

Gradle 依赖项自动为这个项目创建了库,所以我没有复制它.

Gradle dependencies automatically created libraries for this project so I did not duplicate it.

我已启用:构建 > AspectJ 编织.

I have enabled: Build > AspectJ weaving.

设置 > 构建、执行、部署 > 编译器 > Java 编译器:

Settings > Build, Execution, Deployment > Compiler > Java Compiler:

使用编译器:ajc

Ajc 编译路径:/home/me/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjtools/1.8.10/5c5957ee4615bde33b54ce965f5b85b8827b97d5/jars8tools

Path to Ajc compiles: /home/me/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjtools/1.8.10/5c5957ee4615bde33b54ce965f5b85b8827b97d5/aspectjtools-1.8.10.jar

命令行参数:空

生成调试信息:选中

委托给 javac:已检查

Delegate to javac: chcecked

启用注释处理选项:选中

Enable annotation processing options: checked

项目构建和编译没有问题.它运行起来就像从来没有任何方面一样,没有打印出任何方面的特殊性,也没有创建临时文件,并且所有方法都运行良好(尽管在我周围的所有"建议中,我还没有处理联合点).

Project builds and compiles without problem. It runs like there was never any aspect, particularity nothing from aspect is printed out and no temporary files are created, and all methods runs fine (although in my "all" advice that is around I have not processed joint point).

当我在任何建议的第一行创建断点并使用调试器运行项目时,什么也没有发现.

When I create breakpoints in first lines of any advice and run project with debugger nothing is caught.

我错过了什么?

推荐答案

在浪费了更多时间之后,我发现我将两种不同的 AspectJ sytnax 混合在一起.

After wasting even more hours, I found out that I have mixed two distinct AspectJ sytnax together.

一个是关于方面的(比如公共方面 LoggingAspect"),第二个是常规的 Java 注释(比如@Aspect"或@Around").

One that is just about aspects (like "public aspect LoggingAspect"), and second one that are regular Java annotations (like "@Aspect" or "@Around").

当我用public class LoggingAspect"替换public aspect LoggingAspect"时,它起作用了.很难找到,因为一切仍然编译没有问题.

When I have replaced "public aspect LoggingAspect" with "public class LoggingAspect" it worked. It was very hard to find out, because everything still compiles without problem.

希望它在某天或至少在未来版本的 ajc 中对某人有所帮助,他们会添加更详细的编译器输出.

Hope it helps somebody someday or at least in future version of ajc they will add more verbose compiler output.

这篇关于IntelliJ IDEA + AspectJ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆