@AfterThrowing建议未在我的程序中执行。但其他建议已经执行。
这里的方面:
@Component
@Aspect
public class RequestFaildAspect {
static final Logger logger = Logger.getLogger(RequestFaildAspect.class);
//match all public method
@Pointcut("execution(* *(..))")
public void html() {}
@Before("html()")
public void before() {
logger.error("before");
}
@After("html()")
public void after() {
logger.error("after");
}
/**
* 处理Page类的html方法抛出的异常RequestFailedException
*/
@AfterThrowing(pointcut = "html()", throwing = "ex")
public void afterThrowing(JoinPoint point, Throwable ex) {
logger.error("AfterThrowing");
}
}
异常类
package com.jecyhw;
public class Test {
public void test() {
throw new RuntimeException();
}
}
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = com.jecyhw.SpringAopConfig.class)
public class FjsnwDiseaseTest {
com.jecyhw.Test test = new com.jecyhw.Test();
@org.junit.Test
public void test() {
test.test();
}
}
spring aop configure class
@ComponentScan
@Configuration
@EnableAspectJAutoProxy
public class SpringAopConfig {
}
@Before和@After建议可以执行,只有@AfterThrowing不会执行。
弹簧控制台输出,我发现了那些:
13:14:18,381 DEBUG ReflectiveAspectJAdvisorFactory:226 - Found AspectJ method: public void com.jecyhw.request.aspect.RequestFaildAspect.before()
13:14:18,383 DEBUG ReflectiveAspectJAdvisorFactory:226 - Found AspectJ method: public void com.jecyhw.request.aspect.RequestFaildAspect.after()
13:14:18,383 DEBUG ReflectiveAspectJAdvisorFactory:226 - Found AspectJ method: public void com.jecyhw.request.aspect.RequestFaildAspect.afterThrowing(org.aspectj.lang.JoinPoint,java.lang.Throwable)
log4j输出:
2016-08-19 13:14:18 [ main:521 ] - [ ERROR ] before
2016-08-19 13:14:18 [ main:539 ] - [ ERROR ] after
在log4j的输出中,AfterThrowing不输出。
我在afterThrowing的方法和调试中设置了断点,但未使用。