spring-aop代码示例

【README】

  1. 增强器-Advisor: = Pointcut + Advice
    1. 增强点: Pointcut, 切点,在哪里增强;
    2. 增强方法: Advice, 增强逻辑; 
  2. 拦截器MethodInterceptor:运行时执行增强逻辑的统一拦截接口,组成责任链(调用多个拦截器)
  3. 解析切面逻辑的组装步骤:
    1. @Before/@After/@Around
    2. Advice 增强逻辑
    3. Advisor(=Advice + Pointcut); 
    4. AdvisorAdapterRegistry
    5. MethodInterceptor 
    6. ReflectiveMethodInvocationProceed()
    7. 目标方法;

        


【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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值