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

查看:104
本文介绍了使用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类基本上来自称为共享的不同项目,我已经在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天全站免登陆