Spring AOP 自动创建代理

2023-12-19  本文已影响0人  Tinyspot

1. 代理创建器

1.1 三种自动代理创建器

上述3类自动代理器都是基于 BeanPostProcessor 接口的

2. BeanNameAutoProxyCreator

2.1 自定义拦截器链

import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BusinessInterceptorConfig {

    @Bean
    public Advice sessionInterceptor() {
        return new SessionInterceptor();
    }

    @Bean
    public Advice loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
        BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
        // creator.setBeanNames("greetServiceImpl", "greetService");
        creator.setBeanNames("*Impl");
        // 设置拦截链,有顺序
        creator.setInterceptorNames("sessionInterceptor", "loginInterceptor");
        // creator.setProxyTargetClass(true);
        return creator;
    }
}
@Slf4j
public class SessionInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        log.info(this.getClass().getSimpleName() + "; " + invocation.getMethod().getName());
        return invocation.proceed();
    }
}

@Slf4j
public class LoginInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        log.info(this.getClass().getSimpleName() + "; " + invocation.getMethod().getName());
        return invocation.proceed();
    }
}

2.2 自定义拦截器链2

@Configuration
public class BusinessInterceptorConfig {

    @Bean
    public Advice loginInterceptor() {
        return new LoginInterceptor();
    }

    /**
     * @Qualifier 作用:当有多个相同类型的Bean时,可指定实例
     */
    @Bean
    public Advice businessInterceptor(@Qualifier("orderServiceImpl") OrderService orderService) {
        BusinessInterceptor businessInterceptor = new BusinessInterceptor();
        businessInterceptor.setOrderService(orderService);
        return businessInterceptor;
    }

    @Bean
    public BeanNameAutoProxyCreator businessInterceptChain() {
        BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
        creator.setBeanNames("greet*");
        creator.setInterceptorNames("loginInterceptor", "businessInterceptor");
        creator.setProxyTargetClass(true);
        return creator;
    }

}
@Slf4j
public class BusinessInterceptor implements MethodInterceptor {

    private OrderService orderService;

    public OrderService getOrderService() {
        return orderService;
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        log.info(this.getClass().getSimpleName() + "; " + invocation.getMethod().getName() + " before...");

        log.info("执行其他逻辑:" + orderService.totalOrders());

        // 通过反射调用目标方法
        Object obj = invocation.proceed();

        log.info(this.getClass().getSimpleName() + "; " + invocation.getMethod().getName() + " after...");
        return obj;
    }
}

3. DefaultAdvisorAutoProxyCreator

@Configuration
public class BusinessInterceptorConfig {
    @Bean
    public Advisor businessAdvisor() {
        BusinessInterceptor businessInterceptor = new BusinessInterceptor();
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(null, InterceptAnno.class, true);
        advisor.setPointcut(pointcut);
        advisor.setAdvice(businessInterceptor);
        return advisor;
    }

    @Bean
    public Advisor business2Advisor() {
        MyInterceptor myInterceptor = new MyInterceptor();
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(null, InterceptAnno.class, true);
        advisor.setPointcut(pointcut);
        advisor.setAdvice(myInterceptor);
        return advisor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultCreator = new DefaultAdvisorAutoProxyCreator();
        defaultCreator.setAdvisorBeanNamePrefix("business");
        defaultCreator.setUsePrefix(true);
        return defaultCreator;
    }

}
@Slf4j
public class MyInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        log.info(this.getClass().getSimpleName() + "; " + invocation.getThis().getClass().getSimpleName() + "; "
                + invocation.getMethod().getName());
        return invocation.proceed();
    }
}

参考

上一篇下一篇

猜你喜欢

热点阅读