【README】
- 增强器-Advisor: = Pointcut + Advice
- 增强点: Pointcut, 切点,在哪里增强;
- 增强方法: Advice, 增强逻辑;
- 拦截器MethodInterceptor:运行时执行增强逻辑的统一拦截接口,组成责任链(调用多个拦截器)
- 解析切面逻辑的组装步骤:
- @Before/@After/@Around
- Advice 增强逻辑
- Advisor(=Advice + Pointcut);
- AdvisorAdapterRegistry
- MethodInterceptor
- ReflectiveMethodInvocationProceed()
- 目标方法;
【1】spring aspectJ 增强
【业务bean】
@Slf4j
public class TomAddrBean {
private String addr = "ChengDu";
public void test() {
log.info("addr = {}", addr);
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
【切面类】
@Aspect
@Slf4j
public class TomAddrAspect {
@Pointcut("execution(* *.test(..))")
public void test() {
// do nothing.
}
@Before("test()")
public void beforeTest() {
log.info("before test()");
}
@After("test()")
public void afterTest() {
log.info("after test()");
}
@Around("test()")
public Object aroundTest(ProceedingJoinPoint proceedingJoinPoint) {
log.info("around: before test()");
Object result = null;
try {
result = proceedingJoinPoint.proceed();
} catch (Throwable e) {
log.error("test() 目标方法调用异常", e);
throw new RuntimeException("test() 目标方法调用异常");
}
log.info("around: after test()");
return result;
}
}
【 beans0701_aspect.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/aop/spring-context-3.0.xsd
">
<aop:aspectj-autoproxy />
<!-- BeanFactory PostProcessor -->
<bean class="com.tom.srccode.deep.analysis.chapter0501.aop.aspect.TomAddrBean" />
<bean class="com.tom.srccode.deep.analysis.chapter0501.aop.aspect.TomAddrAspect"/>
</beans>
【测试main】
public class TomAddrAspectMain {
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("/a_src_deep_analysis/chapter05/beans0701_aspect.xml");
// 调用目标方法
applicationContext.getBean(TomAddrBean.class).test();
}
}
【运行结果】
- around: before test()
- before test()
- addr = ChengDu
- after test()
- around: after test()

123

被折叠的 条评论
为什么被折叠?



