编程语言
首页 > 编程语言> > java-AOP Spring @AfterReturning无法正常工作

java-AOP Spring @AfterReturning无法正常工作

作者:互联网

我正在学习AOP spring,并尝试一些示例.关于@AfterReturning,我了解的是,仅当目标成功返回且与切入点匹配时才调用该方法.但是在我的情况下,如下所示,我有一个切入点,该切入点定义了仅返回String的所有方法,但是它调用了所有void方法以及返回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类下面找到:

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类下面找到:

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()会被调用,因为它是唯一返回字符串的变量,但是当我运行该应用程序时,却在控制台中获得了以下输出:

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:

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

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

<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方法吗?

我也尝试将afterReturning从String更改为boolean,然后得到仅调用deleteCustomer的预期结果,因为它返回了boolean.

非常感谢您的答复.

解决方法:

返回参数仅指定

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.*(..))

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

您应该将其更改为

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

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

注释值表达式中的返回类型和方法参数类型在将建议哪种方法中都起作用.例如

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

什么都不会匹​​配.

附带说明一下,您应该考虑升级Spring和Aspectj版本.我认为这些问题很多都是固定的,或者整个库都比较稳定.

标签:spring-aop,aop,spring,java
来源: https://codeday.me/bug/20191122/2058933.html