1 16 17 package org.springframework.aop.framework; 18 19 import org.springframework.aop.TargetSource; 20 import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry; 21 import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry; 22 import org.springframework.aop.target.SingletonTargetSource; 23 import org.springframework.beans.factory.BeanClassLoaderAware; 24 import org.springframework.beans.factory.FactoryBean; 25 import org.springframework.beans.factory.FactoryBeanNotInitializedException; 26 import org.springframework.beans.factory.InitializingBean; 27 import org.springframework.util.ClassUtils; 28 29 40 public abstract class AbstractSingletonProxyFactoryBean extends ProxyConfig 41 implements FactoryBean, BeanClassLoaderAware, InitializingBean { 42 43 private Object target; 44 45 private Class [] proxyInterfaces; 46 47 private Object [] preInterceptors; 48 49 private Object [] postInterceptors; 50 51 52 private AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance(); 53 54 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 55 56 private Object proxy; 57 58 59 70 public void setTarget(Object target) { 71 this.target = target; 72 } 73 74 80 public void setProxyInterfaces(Class [] proxyInterfaces) { 81 this.proxyInterfaces = proxyInterfaces; 82 } 83 84 89 public void setPreInterceptors(Object [] preInterceptors) { 90 this.preInterceptors = preInterceptors; 91 } 92 93 103 public void setPostInterceptors(Object [] postInterceptors) { 104 this.postInterceptors = postInterceptors; 105 } 106 107 112 public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) { 113 this.advisorAdapterRegistry = advisorAdapterRegistry; 114 } 115 116 public void setBeanClassLoader(ClassLoader classLoader) { 117 this.beanClassLoader = classLoader; 118 } 119 120 121 public void afterPropertiesSet() { 122 if (this.target == null) { 123 throw new IllegalArgumentException ("'target' is required"); 124 } 125 if (this.target instanceof String ) { 126 throw new IllegalArgumentException ("'target' needs to be a bean reference, not a bean name as value"); 127 } 128 129 ProxyFactory proxyFactory = new ProxyFactory(); 130 131 if (this.preInterceptors != null) { 132 for (int i = 0; i < this.preInterceptors.length; i++) { 133 proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(this.preInterceptors[i])); 134 } 135 } 136 137 proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor())); 139 140 if (this.postInterceptors != null) { 141 for (int i = 0; i < this.postInterceptors.length; i++) { 142 proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(this.postInterceptors[i])); 143 } 144 } 145 146 proxyFactory.copyFrom(this); 147 148 TargetSource targetSource = createTargetSource(this.target); 149 proxyFactory.setTargetSource(targetSource); 150 151 if (this.proxyInterfaces != null) { 152 proxyFactory.setInterfaces(this.proxyInterfaces); 153 } 154 else if (!isProxyTargetClass()) { 155 proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass())); 157 } 158 159 this.proxy = getProxy(proxyFactory); 160 } 161 162 168 protected TargetSource createTargetSource(Object target) { 169 if (target instanceof TargetSource) { 170 return (TargetSource) target; 171 } 172 else { 173 return new SingletonTargetSource(target); 174 } 175 } 176 177 186 protected Object getProxy(AopProxy aopProxy) { 187 return aopProxy.getProxy(this.beanClassLoader); 188 } 189 190 191 public Object getObject() { 192 if (this.proxy == null) { 193 throw new FactoryBeanNotInitializedException(); 194 } 195 return this.proxy; 196 } 197 198 public Class getObjectType() { 199 if (this.proxy != null) { 200 return this.proxy.getClass(); 201 } 202 if (this.proxyInterfaces != null && this.proxyInterfaces.length == 1) { 203 return this.proxyInterfaces[0]; 204 } 205 if (this.target instanceof TargetSource) { 206 return ((TargetSource) this.target).getTargetClass(); 207 } 208 if (this.target != null) { 209 return this.target.getClass(); 210 } 211 return null; 212 } 213 214 public final boolean isSingleton() { 215 return true; 216 } 217 218 219 225 protected abstract Object createMainInterceptor(); 226 227 } 228 | Popular Tags |