美文网首页
Day65 Spring AOP底层源码解析

Day65 Spring AOP底层源码解析

作者: 小周爱吃瓜 | 来源:发表于2022-07-05 11:36 被阅读0次
  • CgLib和JDK 动态代理

  • ProxyFactory

  • DefaultAopProxyFactory

public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {

    @Override
    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);
        }
    }
  • Advice 通知分类
    before,returing,throwing,finally around
  • 让ProxyFactory创建的Bean被Spring所托管
    @Bean
    public ProxyFactoryBean createProxyBean(){
        UserService userService = new UserService();
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setTarget(userService);
        proxyFactoryBean.setInterceptorNames("testAop");
        return proxyFactoryBean;
    }
  • BeanNameAutoProxyCreator 可以指定Bean的name名称

可以对批量的Bean进行Aop.

SpringAop是Spring为了方便我们使用Aop所创造的一套机制.


Aspect是编译时就进行了增强,编译时期就会解析@before这些注解,然后加入到被代理类的字节码文件中。

Aspect编译期,Spring运行时期.

--

  • JBoss aspecjwerkz 等都提供了AOP的支持.

Aspect,JoinPoint,Advice,Target object,Weaving织入(Spring AOP)

  • 利用ProxyFactory生成代理对象,让代理对象执行某个方法的时候,调用targetsource的getTarget方法得到被代理对象.

相关文章

网友评论

      本文标题:Day65 Spring AOP底层源码解析

      本文链接:https://www.haomeiwen.com/subject/vufsbrtx.html