千家信息网

SpringFramework中DefaultAopProxyFactory的作用是什么

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,本篇文章为大家展示了SpringFramework中DefaultAopProxyFactory的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Sp
千家信息网最后更新 2025年02月02日SpringFramework中DefaultAopProxyFactory的作用是什么

本篇文章为大家展示了SpringFramework中DefaultAopProxyFactory的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

Springaop中的DefaultAopProxyFactory,先上一张图,如下图1

图1

Springaop中使用DefaultAopProxyFactory来创建代理,它决定了何时使用JDK还是Cglib代理,实现如下List-1所示:

List-1

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);                }        }        /**         * Determine whether the supplied {@link AdvisedSupport} has only the         * {@link org.springframework.aop.SpringProxy} interface specified         * (or no proxy interfaces specified at all).         */        private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {                Class[] ifcs = config.getProxiedInterfaces();                return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0])));        }}

List-1中,目标对象拥有接口实现且没设置proxyTargetClass=true,则使用JDK代理,此外如果目标对象是个接口或者是代理类,则使用JDK代理,否则使用Cglib,有些人说设置了ProxyTargetClass=true会使用JDK代理,正常情况下是正确的,不过从源码来看不一定--即使设置了ProxyTargetClass=true,如果目标对象类是个接口或者是代理类,则使用使用的是JDK代理。

上述内容就是SpringFramework中DefaultAopProxyFactory的作用是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

0