zoukankan      html  css  js  c++  java
  • aop动态代理源码分析

    package org.springframework.aop.framework;
    import java.io.Serializable;
    import java.lang.reflect.Proxy;

    import org.springframework.aop.SpringProxy;


    @SuppressWarnings("serial")
    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);
    }
    }


    private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
    Class<?>[] ifcs = config.getProxiedInterfaces();
    return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0])));
    }

    }
    config.isOptimize()-------------------------------------------------是否进行优化,默认是false
    config.isProxyTargetClass()-----------------------------------------是否强制使用cglib代理。但它为true也不是肯定就采用cglib,因为下面还有一个判断条件,即目标类是接口,则使用jdk动态代理的方式。
    hasNoUserSuppliedProxyInterfaces(config)----------------------------目标类没有实现接口,或者有但是是接口类型是SpringProxy


    当config.isOptimize(),config.isProxyTargetClass(),hasNoUserSuppliedProxyInterfaces(config)全部为false时候使用JDK动态代理JdkDynamicAopProxy
    当config.isOptimize(),config.isProxyTargetClass(),hasNoUserSuppliedProxyInterfaces(config)有一个为true,且代理对象不为接口时或者配置注解
    @EnableAspectJAutoProxy(proxyTargetClass = true),使用cglib动态代理

     

  • 相关阅读:
    #Leetcode# 219. Contains Duplicate II
    #Leetcode# 203. Remove Linked List Elements
    #Leetcode# 141. Linked List Cycle
    #Leetcode# 128. Longest Consecutive Sequence
    linux——shell解释
    Linux——互联网搜索引擎nbtscan是一个扫描WINDOWS网络NetBIOS信息的小工具
    Linux——网络端口的状态netstat、ifconfig
    连不上网的原因
    jquery基础笔记
    网址收藏
  • 原文地址:https://www.cnblogs.com/WQLLL/p/11073388.html
Copyright © 2011-2022 走看看