首页 \ 问答 \ IntelliJ IDEA + AspectJ(IntelliJ IDEA + AspectJ)

IntelliJ IDEA + AspectJ(IntelliJ IDEA + AspectJ)

我试图在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,它正确提示所有方法(getter和setter)都是我的切入点。 它也暗示了我的建议“登录”在我的方面有多个切入点。 但是,它没有暗示“所有”的建议。

我安装了(最新版本)插件,如: - 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

启用注释处理选项:选中

项目构建和编译没有问题。 它的运行方式从来没有任何方面,特殊性方面没有打印出来,并没有创建临时文件,所有方法运行良好(虽然在我的“所有”的建议,我没有处理联合点)。

当我在任何建议的第一行创建断点并用调试器运行项目时,什么都不会被捕获。

我错过了什么?


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.

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

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'

My aspect:

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;
    }
}

Sample class:

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;
    }

...
}

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.

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

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

I have enabled: Build > AspectJ weaving.

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

Use compiler: ajc

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

Command line parameters: empty

Generate debug info: checked

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.

What am I missing?


原文:https://stackoverflow.com/questions/44088421
更新时间:2023-12-16 17:12

最满意答案

Northwind db = new Northwind();

或者使类静态,您不需要自己初始化。

static class Northwind
{
    public static Queue<Shipper> Queue { get; set; }

    public static List<Shipper> GetList()
    {
        var con = new SqlConnection("Data Source=DESKTOP-G5VBFCN;Initial Catalog=Northwind;Integrated Security=True");
        var cmd = new SqlCommand("SELECT CompanyName, Phone FROM Shippers",con);
        con.Open();
        var reader = cmd.ExecuteReader();
        var ShipList = new List<Shipper>();

        while (reader.Read())
        {
            var s = new Shipper
            {
                CompanyName = reader["CompanyName"].ToString(),
                Phone = reader["Phone"].ToString()
            };
            ShipList.Add(s);
        }
        con.Close();
        return ShipList;
    }

    public static Queue<Shipper> GetQueue(List<Shipper> List)
    {
        Queue<Shipper> ShipperQueue = new Queue<Shipper>(List);
        return ShipperQueue;
    }
}

并调用

private void Form1_Load(object sender, EventArgs e)
{
    Northwind.Queue = Northwind.GetQueue(Northwind.GetList());
}

Northwind db = new Northwind();

Or make the class static and you don't need to initialize on your own.

static class Northwind
{
    public static Queue<Shipper> Queue { get; set; }

    public static List<Shipper> GetList()
    {
        var con = new SqlConnection("Data Source=DESKTOP-G5VBFCN;Initial Catalog=Northwind;Integrated Security=True");
        var cmd = new SqlCommand("SELECT CompanyName, Phone FROM Shippers",con);
        con.Open();
        var reader = cmd.ExecuteReader();
        var ShipList = new List<Shipper>();

        while (reader.Read())
        {
            var s = new Shipper
            {
                CompanyName = reader["CompanyName"].ToString(),
                Phone = reader["Phone"].ToString()
            };
            ShipList.Add(s);
        }
        con.Close();
        return ShipList;
    }

    public static Queue<Shipper> GetQueue(List<Shipper> List)
    {
        Queue<Shipper> ShipperQueue = new Queue<Shipper>(List);
        return ShipperQueue;
    }
}

and invoke like

private void Form1_Load(object sender, EventArgs e)
{
    Northwind.Queue = Northwind.GetQueue(Northwind.GetList());
}

相关问答

更多
  • 大多数OOP概念与语言无关,但由于PHP不是最强大的OO语言,您是否考虑过使用其他语言学习[Java,Smalltalk等]? 一旦你掌握了这些概念,它就会成为大多数情况下查找php等效语法的问题。 I ended up using a few books, PHP for Absolute Beginners and Object Oriented PHP by Peter Lavin.
  • 你可以从多个角度看这个。 其他人会不同意,但我认为OOP接口是从类型类开始的好地方(当然比起从没有开始)。 人们喜欢在概念上指出,类型类分类类型,很像集 - “支持这些操作的一组类型,以及其他不能在语言本身编码的期望”。 这是有道理的,偶尔做一个没有方法声明一个类类,说“只有当你的类型是这个类的一个实例,如果它符合某些要求”。 OOP接口很少出现这种情况。 在具体差异方面,类型类有多种方式比OOP接口更强大: 最大的一个是,类型类将类型实现接口的声明与类型本身的声明分离。 使用OOP接口,您列出了类型在您定 ...
  • Northwind db = new Northwind(); 或者使类静态,您不需要自己初始化。 static class Northwind { public static Queue Queue { get; set; } public static List GetList() { var con = new SqlConnection("Data Source=DESKTOP-G5VBFCN;Initial Catal ...
  • 由此改变: function getPage() { if(page === 'home') { 为此: function getPage() { if($this->page === 'home') { 错误消息“注意:使用未定义的常量页面 - 假设'页面'”并不是非常有帮助,但这是由于不幸的事实,即PHP将隐式地将未知标记转换为具有相同值的常量。 这就是它看到的page (它没有在它前面的$ ,因此不是一个变量名),并把它看作好像有一个先前的语句define('page', 'pag ...
  • 您的第一个代码片段实际上是一个穷人在非OO语言中的OO实现的示例。 您正在定义抽象数据类型( vector_t )及其上允许的所有操作( setVector , addVector等),但您并未将所有数据和操作封装到单个逻辑单元(即类)中。 如果您想要或需要使用C而不是C ++但仍希望获得OOP的一些好处,这可能很有用。 既然你已经在两个例子中都在进行OOP,我认为为什么第二个代码片段更好是显而易见的。 Your first code snippet is actually an example of a ...
  • 如评论中所述 protected $db; public function __construct($dbname, $dbuser, $dbpass, $dbtype = 'oci') { $this->db = new PDO($dbtype.':dbname='.$dbname, $dbuser, $dbpass); } $ this:指类的实例 as mentioned in the comments protected $db; public function __construct($ ...
  • http://php.net/manual/en/function.mssql-connect.php 第四个param new_link必须作为true传递(因为默认值为false )。 http://php.net/manual/en/function.mssql-connect.php There is fourth param new_link that must be passed as true (because default is false).
  • OOP与结构化编程有用的方式非常有用。 您不需要使用函数,因为您可以使用goto语句编写程序来跳转,但这会使代码难以维护和思考。 类似地,OOP将函数和变量组合到对象中,允许您进一步分解代码,并且通过添加抽象,您可以更轻松地思考代码并以更易于维护的方式编写代码。 OOP is useful in the same way structured programming is useful. You don't need to use functions since you can write a progra ...
  • 那么编写自己的MVC框架呢? 不必是任何巨大的东西,但你可以研究其他框架并选择你最喜欢的位,然后创建自己的。 如果动机是为了学习而你在php中编程,那么一个能让你深入了解MVC框架功能的项目只会对你有益。 它也是您可以随时构建的项目类型。 What about writing your own MVC framework? Doesn't have to be anything huge, but you could research other frameworks out there and pick ...
  • this是JavaScript中的一个棘手的野兽,正如其他人指出的那样是问题的关键。 在任何地方使用它的问题在于它的值可以根据调用函数的人/位置而改变(例如,在JavaScript中查看call和apply方法)。 我猜测如果你在chrome.tabs.getSelected函数的回调中将this值写入控制台,你会发现它不再是你的矿工了。 解决方案是捕获this实际感兴趣的引用,当您确定它是正确的时候然后再使用该引用。 在你的例子中看到它在线评论可能更有意义: function SocialMiner(ta ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。