AOP Spring @AfterReturning 没有按预期工作 [英] AOP Spring @AfterReturning not working as expected

查看:34
本文介绍了AOP Spring @AfterReturning 没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 AOP spring 并尝试一些示例.关于@AfterReturning,我所了解的是,只有成功返回目标并且与切入点匹配时才会调用该方法.但是,在我的情况下,如下所示,我有一个切入点,它定义了所有仅返回 String 的方法,但它正在调用所有 void 方法以及返回 String 的方法.

I am learning AOP spring and trying out some examples. Regarding @AfterReturning,what i have understand is that the method is called only if the target is returned successfully and which match the pointcut. However in my case as shown below i have a pointcut that defines all method that returns a String only but it is calling all the void method and also the method which return a String.

我的建议:

@AfterReturning(value= "execution(*  com.aop..CustomerServiceImpl.*(..))",
        returning= "string")
public void returnStringPointcut(JoinPoint joinPoint,String string){

    System.out.println("logAfter() is running!");
    System.out.println("String : " + string);
    System.out.println("hijacked : " + joinPoint.getSignature().getName());
    System.out.println("******");

}

请在下面找到我的 Impl 类:

Please find below my Impl class below:

public void addCustomer() {
    // TODO Auto-generated method stub

}

public String getCustomer() {
    // TODO Auto-generated method stub
    return "string";
}

public boolean deleteCustomer() {
    // TODO Auto-generated method stub
    return false;
}
public void addCustomerAppended() {
    // TODO Auto-generated method stub

}

public void deleteCustomerVoid() {
    // TODO Auto-generated method stub
    //return false;
}

请在我的 MainApp 类下面找到:

Please find below my MainApp class:

public class App {
    public static void main(String[] args)

    {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Spring-Customer.xml");

        CustomerService cs =  context.getBean("customerBo", CustomerService.class);
        cs.addCustomer();
        cs.addCustomerAppended();
        cs.deleteCustomer();
        cs.deleteCustomerVoid();
        cs.getCustomer();


    }
}

我原以为只有 getCustomer() 会被调用,因为它是唯一一个返回字符串的函数,但是当我运行应用程序时,我在控制台中得到以下输出:

I was expecting that only getCustomer() will be called since it is the only one that returns a string but instead i get the following output in my console when i run the application:

logAfter() is running!
String : null
hijacked : addCustomer
******
logAfter() is running!
String : null
hijacked : addCustomerAppended
******
logAfter() is running!
String : null
hijacked : deleteCustomerVoid
******
logAfter() is running!
String : string
hijacked : getCustomer
******

请在下面找到我的 pom.xml:

Please find my pom.xml below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aop.spring</groupId>
    <artifactId>SpringAopOnly</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringAopOnly</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring AOP  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

            <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.11</version>
            </dependency>

    </dependencies>
</project>

请在下面找到我的配置文件:

Please find my config file below:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <!-- <aop:aspectj-autoproxy />  -->
    <aop:aspectj-autoproxy>
    <aop:include name ="logAspect" />
    </aop:aspectj-autoproxy>

    <bean id="customerBo" class="com.aop.impl.CustomerServiceImpl" />

    <!-- Aspect -->
    <bean id="logAspect" class="com.aop.aspect.CustomerAspect" />

</beans>

知道它为什么调用 void 方法吗?

Any idea why it is calling the void method please?

我还尝试将 afterReturning 从 String 更改为 boolean 然后我得到了预期的结果,即调用 deleteCustomer 因为它返回一个布尔值.

I have also tried to change the afterReturning from String to boolean then i get the expected result that is only deleteCustomer is called since it returns a boolean.

非常感谢您的回复.

推荐答案

returning 参数仅指定

通知签名中绑定返回的参数的名称价值到

The name of the argument in the advice signature to bind the returned value to

你的实际切入点

@AfterReturning(value= "execution(*  com.aop..CustomerServiceImpl.*(..))",
    returning= "string")

正在指定

execution(* com.aop..CustomerServiceImpl.*(..))

其中 * 匹配所有返回类型.

where * matches all return types.

你应该把它改成

execution(java.lang.String com.aop..CustomerServiceImpl.*(..))

如果您只需要声明为返回 String 的方法.

if you want only methods declared as returning a String.

注解value 表达式中的返回类型和方法参数类型都在将要建议的方法中发挥作用.例如

Both the return type in the annotation value expression and the method parameter type play a role in what methods are going to be advised. For example

@AfterReturning(value = "execution(String com.aop..CustomerServiceImpl.*(..))", returning = "random")
public void returnStringPointcut(JoinPoint joinPoint, Integer random) {

不会匹配任何东西.

附带说明,您应该考虑升级 Spring 和 aspectj 版本.我认为很多这些问题要么已经解决,要么整体库更加稳定.

On a side note, you should consider upgrading your Spring and aspectj versions. I think a lot of these issues are either fixed or the overall libraries are more stable.

这篇关于AOP Spring @AfterReturning 没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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