Spring AOP @AfterThrowing Advice not being executed

ぃ、小莉子 提交于 2019-12-12 04:35:20

问题


The @AfterThrowing advice I have defined is not getting executed. Could some one take a look and please help me out? Below is the code I am using.

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>Test</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringAOPAfterThrowsTest</name>
<dependencies>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>3.2.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>3.2.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>3.2.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.7.3</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.7.3</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>3.2.4.RELEASE</version>
 </dependency>
</dependencies>
</project>

spring-context.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <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"
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/aop  

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.2.xsd">


<aop:aspectj-autoproxy />
<bean id="loggingBean" class="com.aop.test.LoggingAspect" />

</beans>

LoggingAspect.java

package com.aop.test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;


@Aspect
public class LoggingAspect {

@AfterThrowing(pointcut = "execution(* com.aop..*.*(..))", throwing = "e")
public void afterThrowingAdvice(final JoinPoint jp, final Exception e) {
    System.out.println("Exception is " + e.getLocalizedMessage());
    System.out.println("Annotation driven:After throwing " +    
jp.getSignature().getName()
            + "()");

}
}

SampleClassUnderAOP

package com.aop.test;

public class SampleClassUnderAOP {

public void sampleMethod() {
    String str = null;
    System.out.println(str.length());
}

}

TestAOP unit test class

package com.aop.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:/spring-context.xml" })
public class TestAOP {

    @Test
    public void test() {
        SampleClassUnderAOP sc = new SampleClassUnderAOP();
        sc.sampleMethod();
    }

}

回答1:


In your LoggingAspect try following:

@AfterThrowing(pointcut = "execution(public * com.aop..*(..))", throwing = "e")

Removed the .*



来源:https://stackoverflow.com/questions/19386858/spring-aop-afterthrowing-advice-not-being-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!