失效链接处理 |
SpringAop的简单理解 PDF 下载
本站整理下载:
相关截图:
![]()
主要内容:
什么是Spring AOP?
Spring AOP与AspectJ的目的一致,都是为了统一处理横切业务,和AspectJ不同的是,Spring AOP采用动态代理技
术构建Spring AOP的内部机制,而AspectJ采用编译器织入和类装载期织入。
开启切面
要在项目上使用AOP,那么前提一定要先通知Spring开启支持aspectj代理的配置,开启的方式有两种:
基于XML配置: <aop:aspectj-autoproxy />
基于注解配置: @EnableAspectJAutoProxy @EnableAspectJAutoProxy 都做了哪些?
首先Spring容器初始化会通过@ComponentScan扫描到@Service
接着会扫描@EnableAspectJAutoProxy注解中的@Import注解中的类
扫描完后会分别把 EnableAspectJAutoProxy 和 AspectJAutoProxyRegistrar 注册成BeanDefinition
继续循环 AspectJAutoProxyRegistrar 类中是否有 @Component 、 @ComponentScan 、 @Import 等注
解,如果有继续注册成BeanDefinition对象
@EnableAspectJAutoProxy 注解源码
@Service /** TODO: * 开启注解AOP * */ @EnableAspectJAutoProxy(proxyTargetClass = false,exposeProxy = true) public class EnableAspectJAutoProxyBean { }@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(AspectJAutoProxyRegistrar.class) public @interface EnableAspectJAutoProxy { /*** true: * 1、目标对象实现了接口 – 使用CGLIB代理机制 * 2、目标对象没有接口(只有实现类) – 使用CGLIB代理机制 ** false: * 1、目标对象实现了接口 – 使用JDK动态代理机制(代理所有实现了的接口) * 2、目标对象没有接口(只有实现类) – 使用CGLIB代理机制 **/
切面的常用术语
boolean proxyTargetClass() default false; /*** 是否由Spring AOP 暴露代理(代理对象用ThreadLocal存起来) */ boolean exposeProxy() default false;
|