【问题标题】:Spring AOP @AfterThrowing Advice not being executedSpring AOP @AfterThrowing Advice 没有被执行
【发布时间】:2013-10-23 14:09:44
【问题描述】:

我定义的@AfterThrowing 建议没有被执行。有人可以看一下,请帮助我吗?下面是我正在使用的代码。

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 单元测试类

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();
    }

}

【问题讨论】:

    标签: exception aop aspectj spring-aop spring-annotations


    【解决方案1】:

    在您的LoggingAspect 中尝试以下操作:

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

    删除了.*

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多