问题
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.
My Advice:
@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("******");
}
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;
}
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();
}
}
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
******
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>
Any idea why it is calling the void method please?
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.
Thanks a lot in advance for response.
回答1:
The returning
argument only specifies
The name of the argument in the advice signature to bind the returned value to
Your actual pointcut
@AfterReturning(value= "execution(* com.aop..CustomerServiceImpl.*(..))",
returning= "string")
is specifying
execution(* com.aop..CustomerServiceImpl.*(..))
where *
matches all return types.
You should change it to
execution(java.lang.String com.aop..CustomerServiceImpl.*(..))
if you want only methods declared as returning a String
.
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) {
won't match anything.
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.
来源:https://stackoverflow.com/questions/20726737/aop-spring-afterreturning-not-working-as-expected