深刻理解Spring AOP之二代理对象生成
spring代理对象
Spring AOP 使用类 org.springframework.aop.framework.ProxyFactory进行织入。找到ProxyFactory相应的相关内容,而后整理例如如下类图java
Spring代理类怎样生成
先看ProxyFactory是怎样获得代理类的
public Object getProxy() { return createAopProxy().getProxy(); } protected final synchronized AopProxy createAopProxy() { if (!this.active) { activate(); } return getAopProxyFactory().createAopProxy(this); }
ProxyFactoryBean是怎样得到代理类的
因此。假设容器中某个对象依赖于ProxyFactoryBean,那么它将会使用到ProxyFactoryBean的getObject()方法所返回的代理对象 git
@Override public Object getObject() throws BeansException { initializeAdvisorChain(); //初始化通知器 if (isSingleton()) { return getSingletonInstance();//依据定义生成单例的Proxy } else { if (this.targetName == null) { logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " + "Enable prototype proxies by setting the 'targetName' property."); } return newPrototypeInstance(); //这里依据定义生成prototype的Proxy } }
private synchronized Object getSingletonInstance() { if (this.singletonInstance == null) { this.targetSource = freshTargetSource();//返回被 代 理的 目标对象 if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) { //从targetSource中获取目标对象的Class Class<?> targetClass = getTargetClass(); if (targetClass == null) { throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy"); } //这里设置代理对象的接口 setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader)); } // 初始化共享的单例 super.setFrozen(this.freezeProxy); //这里会使用ProxyFactory来生成需要的Proxy this.singletonInstance = getProxy(createAopProxy()); } return this.singletonInstance; }
protected final synchronized AopProxy createAopProxy() { if (!this.active) { activate(); } return getAopProxyFactory().createAopProxy(this); //这里借助了AopProxyFactory }
public AopProxyFactory getAopProxyFactory() { return this.aopProxyFactory; }
private AopProxyFactory aopProxyFactory; public ProxyCreatorSupport() { this.aopProxyFactory = new DefaultAopProxyFactory(); }
github
@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); } }
@Override public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource()); } //依据advised 中 的 配 置信息,将proxy需要代 理的接口放入proxiedInterfaces 中 Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised); findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); //如下这种方法眼熟吧,哈哈 没错就是JDK中的动态代理经典方法 return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); }
总结
第一种获取比較简单。但是需要手工的进行写代码,而另一种是经过Spring的IOC机制来控制Bean的生成。 spring