spring AOP实现原理,

spring 会在初始化的时候,创建一个BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator)用来为类注入切面。

AnnotationAwareAspectJAutoProxyCreator 是怎么加载到spring中的?

1. @EnableAutoConfiguration

2. AOPautoConfiguration

@Configuration
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class,
AnnotatedElement.class })
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration { @Configuration
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
public static class JdkDynamicAutoProxyConfiguration { } @Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
public static class CglibAutoProxyConfiguration { } }

3. 注入的过程

    @Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry) {
return registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry, null);
} @Nullable
public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry,
@Nullable Object source) { return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source);
}

这个类的作用:

1.拦截 advisor 类。(建议类,为bean加载切面:包括自定的advisor,和transactionAdvisor)

AbstractAutoProxyCreator.java

    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName); if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}

2.拦截带有切面注解的类,和切面定义拦截的类。

    public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) throws BeansException {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this.earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
    protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
} // Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
} this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}

3.创建代理类

  3.1根据proxyFactory 获取对应的代理类

    public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}

  3.2 使用proxy类,创建 proxyObject  (和cglib的用法基本一样。)

            enhancer.setSuperclass(proxySuperClass);
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader)); Callback[] callbacks = getCallbacks(rootClass);
Class<?>[] types = new Class<?>[callbacks.length];
for (int x = 0; x < types.length; x++) {
types[x] = callbacks[x].getClass();
}
// fixedInterceptorMap only populated at this point, after getCallbacks call above
enhancer.setCallbackFilter(new ProxyCallbackFilter(
this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
enhancer.setCallbackTypes(types); // Generate the proxy class and create a proxy instance.
return createProxyClassAndInstance(enhancer, callbacks);

4.targetSource 的作用和原理

targetSource 是一个 被代理的对象,用来封装具体的代理对象。所有生成后的代理对象,1.调用切面,2调用targetSource.getTarget(),3.会调用method.Invoke(target,args)

用处:

1.热交换  JndiObjectTargetSource  加载spring时,tomcat先加载了dataSource,此时对已有的dataSource加切面是不和实际的。所以可以对JNDIdatasource加切面,在调用具体的方法时,由targetSource交换的通过jndi查找到的数据源。

为了兼容targetSource有了

SingletonTargetSource,EmptyTargetSource,

springAOP实现原理的更多相关文章

  1. OOP的完美点缀—AOP之SpringAOP实现原理

    OOP的完美点缀-AOP之SpringAOP实现原理 前言 OOP与AOP OOP(Object Oriented Programming,面向对象编程),通过封装.继承将程序抽象为各个层次的对象,进 ...

  2. SpringAOP实现(原理)

    AOP原理: AOP分为:JDK动态代理和CGLIB代理 静态代理:由程序员创建或特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了.              注 ...

  3. spring框架学习笔记4:SpringAOP实现原理

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  4. SpringAOP 设计原理

    1.  设计原理 引入了,代理模式. java 程序执行流: 如果从虚拟机的角度看,整个程序的过程就是方法的调用,我们按照方法的执行顺序,将方法调用成一串. 在方法之间有着Join Point 连接点 ...

  5. SpringAop实现原理及代理模式

    Spring Aop的原理 Spring的AOP就是通过动态代理实现的.当为某个Bean或者某些Bean配置切面时,Spring会为其创建代理对象,当调用该对象的某个方法时,实际是调用生成的代理类的对 ...

  6. springAOP配置原理

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...

  7. SpringAOP 原理解析

    什么是AOP? 1: 传统的OOP编程他的代码逻辑是一种自上向下, 而在这些自上而下的过程中会产生一些横切性的问题,比如说:日志信息,权限校验认证,事务等, 2: 这些横切性问题,往往与我们的主业务逻 ...

  8. Spring知识点总结(四)之SpringAOP基础

        1. Spring aop中的基本概念        • 连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候.在Spring AOP中,一个连接 ...

  9. 75道阿里Java面试题,你能答上几道?

    整理了下阿里近几年的java面试题目,大家参考下吧,希望对大家有帮助,可以帮大家查漏补缺. 答对以下这些面试题,可以淘汰掉 80 % 的求职竞争者. 1.hashcode相等两个类一定相等吗?equa ...

随机推荐

  1. kali linux2019.4安装启动后中文乱码

    1.鼠标右键找到黑框框打开终端 2.终端执行后重启,乱码解决. sudo apt-get install ttf-wqy-zenhei

  2. AcWing 338. 计数问题

    #include <iostream> #include <algorithm> #include <vector> using namespace std; ; ...

  3. 浏览器的主要构成High Level Structure

    浏览器的主要组件包括: 1.     用户界面- 包括地址栏.后退/前进按钮.书签目录等,也就是你所看到的除了用来显示你所请求页面的主窗口之外的其他部分 2.     浏览器引擎- 用来查询及操作渲染 ...

  4. Pacemaker+ISCSI实现Apache高可用-配置

    一.配置文件系统 任意节点用ISCSI的共享磁盘创建LVM node1 pvcreate /dev/sdb vgcreate my_vg /dev/sdb lvcreate -L 1G -n web_ ...

  5. Mysql 函数定义及批量数据脚本

    零.说在前面 在定义函数之前 需要先将 log_bin_trust_function_creators 值设为开启,原因如下 在主从复制的两台Mysql服务器中,slaver会从master复制数据, ...

  6. Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)

    This problem is different from the hard version. In this version Ujan makes exactly one exchange. Yo ...

  7. vue 生命钩子周期之理解

    对于vue的初学者来说,理解vue的生命钩子周期是很有必要的.什么是生命钩子周期呢,顾名思义就是 “实例初始化”  到  “实例被销毁” 的过程. 理解vue的生命钩子周期,我们就可以更好的在项目中运 ...

  8. vue+vuex项目中怎么实现input模糊查询

    1,首先给input框添加方法,但是用的是element-ui的组件,对input进行了封装,不能直接用原生的方法!,在element组件中,input框中方法有实例参数$event,代表事件对象  ...

  9. InkWell容器 和 官方自带日期组件 和第三方 日期格式转换组件

    带点击事件的容器 InkWell( child: Text('时间'), onTap: _showTimePicker,),   Flutter 日期和时间戳 日期转化成时间戳: var now = ...

  10. django urls.py 中的name 使用方法

    使用场景: 当我们在url的时候,一般情况下都是使用很明确的url地址.如在网页里面使用<a href="/login">登录</a>.像这样的链接有很 多 ...