使用 AspectJ 获取对象实例化并访问其相关属性和方法 [英] Get Object Instantiation and Access Its Related Attributes and Methods with AspectJ

查看:30
本文介绍了使用 AspectJ 获取对象实例化并访问其相关属性和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用 AspectJ 创建或实例化对象并访问对象的属性和方法吗?我知道使用 AspectJ 我们可以获取方法的输入参数,但它与创建对象是否相同?

Can we get an object created or instantiated using AspectJ and access object's attributes and methods? I know with AspectJ we can get input parameters of the methods but will it be the same with the object creation?

例如,在代码中有这一行

For example, in the code it has this line

testing test = new testing();

我想使用 AspectJ 获取测试对象并访问类 testing() 的方法和变量,如方法 aa().

And I would like to get the test object using AspectJ and access the method and variables of class testing(), like method aa().

这是测试类的代码.

package testMaven;

public class testing {

    public int dd;


    public int getDd() {
        return dd;
    }


    public void setDd(int dd) {
        this.dd = dd;
    }


    public void aa(int a){
        System.out.println(a);
    }
}

真正的编码问题

上面的编码只是我目前遇到的抽象问题的一个例子.感谢下面的答案,它在这个例子中有效,但在我拥有的实际项目中它不起作用.基本上代码如下:

The above coding is only merely an example of the abstract problem I currently have the problem. Thanks to the answer below, it works in this example, but in the real project that I have it is not working. Basically the code as follow:

@After("execution(de.hpi.cloudraid.dto.control.storedObjectService.CreateFileFinalResponseDto.new(..))")
    public void constructorInvocation(JoinPoint joinPoint) throws Throwable {
        CreateFileFinalResponseDto instance = (CreateFileFinalResponseDto) joinPoint.getTarget();
        System.err.println("testing new object creation");
        System.err.println("id upload = " + instance.getFileObjectID());

    }

但是代码不起作用,因为它给出了未应用建议的警告(xlint:adviceDidNotMatch).CreateFileFinalResponseDto 类基本上来自不同的名为 shared 的项目,我已经在 pom.xml 和 AspectJ 依赖项和插件中包含了共享项目,如下所示:

But the code is not working as it gives the warning of advice has not been applied (xlint : adviceDidNotMatch). CreateFileFinalResponseDto class is basically from different project called shared and I already include the shared project in the pom.xml and AspectJ dependencies and plugin as follow:

   <dependencies>
            <!-- Project -->
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>shared</artifactId>
                <version>${project.server.version}</version>
            </dependency> 
    ...
    <!-- AspectJ -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjtools</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
...

     <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.8</version>
                <configuration>
                    <complianceLevel>${java.version}</complianceLevel>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <showWeaveInfo>true</showWeaveInfo>
                </configuration>
                <executions>
                    <execution>
                        <id>AspectJ-Classes</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>AspectJ-Test-Classes</id>
                        <phase>process-test-classes</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

感谢任何帮助.谢谢

推荐答案

要捕获类实例化可以使用以下方面:

To catch class instantiate one could use the following aspect:

@Aspect
public class Interceptor {
    /**
     * Matches constructor testMaven.testing.
     * <p/>
     * *NOTE:* This will only work when class compiled with aspectj.
     */
    @After("execution(* testMaven.testing.new(..))")
    public void constructorInvocation(JoinPoint joinPoint)
            throws Throwable {
        testing instance = (testing) joinPoint.getTarget();
        instance.aa(2);
    }
}

如果您想捕获实例化Before,当然不会有实例.

In case you want to catch the instantiate Before there won't be a instance of course.

这篇关于使用 AspectJ 获取对象实例化并访问其相关属性和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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