1 16 17 package org.springframework.aop.scope; 18 19 import java.lang.reflect.Modifier ; 20 21 import org.springframework.aop.framework.AopInfrastructureBean; 22 import org.springframework.aop.framework.ProxyConfig; 23 import org.springframework.aop.framework.ProxyFactory; 24 import org.springframework.aop.support.DelegatingIntroductionInterceptor; 25 import org.springframework.aop.target.SimpleBeanTargetSource; 26 import org.springframework.beans.factory.BeanFactory; 27 import org.springframework.beans.factory.BeanFactoryAware; 28 import org.springframework.beans.factory.FactoryBean; 29 import org.springframework.beans.factory.FactoryBeanNotInitializedException; 30 import org.springframework.beans.factory.config.ConfigurableBeanFactory; 31 import org.springframework.util.ClassUtils; 32 33 52 public class ScopedProxyFactoryBean extends ProxyConfig implements FactoryBean, BeanFactoryAware { 53 54 55 private final SimpleBeanTargetSource scopedTargetSource = new SimpleBeanTargetSource(); 56 57 58 private String targetBeanName; 59 60 61 private Object proxy; 62 63 64 67 public ScopedProxyFactoryBean() { 68 setProxyTargetClass(true); 69 } 70 71 72 75 public void setTargetBeanName(String targetBeanName) { 76 this.targetBeanName = targetBeanName; 77 this.scopedTargetSource.setTargetBeanName(targetBeanName); 78 } 79 80 public void setBeanFactory(BeanFactory beanFactory) { 81 if (!(beanFactory instanceof ConfigurableBeanFactory)) { 82 throw new IllegalStateException ("Not running in a ConfigurableBeanFactory: " + beanFactory); 83 } 84 ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory; 85 86 this.scopedTargetSource.setBeanFactory(beanFactory); 87 88 ProxyFactory pf = new ProxyFactory(); 89 pf.copyFrom(this); 90 pf.setTargetSource(this.scopedTargetSource); 91 92 Class beanType = beanFactory.getType(this.targetBeanName); 93 if (beanType == null) { 94 throw new IllegalStateException ("Cannot create scoped proxy for bean '" + this.targetBeanName + 95 "': Target type could not be determined at the time of proxy creation."); 96 } 97 if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) { 98 pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType)); 99 } 100 101 ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName()); 103 pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject)); 104 105 pf.addInterface(AopInfrastructureBean.class); 108 109 this.proxy = pf.getProxy(cbf.getBeanClassLoader()); 110 } 111 112 113 public Object getObject() { 114 if (this.proxy == null) { 115 throw new FactoryBeanNotInitializedException(); 116 } 117 return this.proxy; 118 } 119 120 public Class getObjectType() { 121 if (this.proxy != null) { 122 return this.proxy.getClass(); 123 } 124 if (this.scopedTargetSource != null) { 125 return this.scopedTargetSource.getTargetClass(); 126 } 127 return null; 128 } 129 130 public boolean isSingleton() { 131 return true; 132 } 133 134 } 135 | Popular Tags |