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动态代理

     

  • 相关阅读:
    POJ1985 树的直径(BFS
    POJ2186 强连通分量+缩点
    AIM Tech Round 5C. Rectangles 思维
    POJ2553 汇点个数(强连通分量
    hdu6370 并查集+dfs
    UVALive 7037:The Problem Needs 3D Arrays(最大密度子图)
    POJ 3155:Hard Life(最大密度子图)
    HDU 5527:Too Rich(DFS+贪心)***
    HDU 5534:Partial Tree(完全背包)***
    Wannafly挑战赛1:Treepath(DFS统计)
  • 原文地址:https://www.cnblogs.com/WQLLL/p/11073388.html
Copyright © 2011-2022 走看看