org.springframework.aop.scope
public class: ScopedProxyFactoryBean [javadoc |
source]
java.lang.Object
org.springframework.aop.framework.ProxyConfig
org.springframework.aop.scope.ScopedProxyFactoryBean
All Implemented Interfaces:
BeanFactoryAware, FactoryBean, Serializable
Convenient proxy factory bean for scoped objects.
Proxies created using this factory bean are thread-safe singletons
and may be injected into shared objects, with transparent scoping behavior.
Proxies returned by this class implement the ScopedObject interface.
This presently allows for removing the corresponding object from the scope,
seamlessly creating a new instance in the scope on next access.
Please note that the proxies created by this factory are
class-based proxies by default. This can be customized
through switching the "proxyTargetClass" property to "false".
| Methods from org.springframework.aop.framework.ProxyConfig: |
|---|
|
copyFrom, isExposeProxy, isFrozen, isOpaque, isOptimize, isProxyTargetClass, setExposeProxy, setFrozen, setOpaque, setOptimize, setProxyTargetClass, toString |
| Method from org.springframework.aop.scope.ScopedProxyFactoryBean Detail: |
public Object getObject() {
if (this.proxy == null) {
throw new FactoryBeanNotInitializedException();
}
return this.proxy;
}
|
public Class getObjectType() {
if (this.proxy != null) {
return this.proxy.getClass();
}
if (this.scopedTargetSource != null) {
return this.scopedTargetSource.getTargetClass();
}
return null;
}
|
public boolean isSingleton() {
return true;
}
|
public void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof ConfigurableBeanFactory)) {
throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
}
ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
this.scopedTargetSource.setBeanFactory(beanFactory);
ProxyFactory pf = new ProxyFactory();
pf.copyFrom(this);
pf.setTargetSource(this.scopedTargetSource);
Class beanType = beanFactory.getType(this.targetBeanName);
if (beanType == null) {
throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName +
"': Target type could not be determined at the time of proxy creation.");
}
if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) {
pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader()));
}
// Add an introduction that implements only the methods on ScopedObject.
ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName());
pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));
// Add the AopInfrastructureBean marker to indicate that the scoped proxy
// itself is not subject to auto-proxying! Only its target bean is.
pf.addInterface(AopInfrastructureBean.class);
this.proxy = pf.getProxy(cbf.getBeanClassLoader());
}
|
public void setTargetBeanName(String targetBeanName) {
this.targetBeanName = targetBeanName;
this.scopedTargetSource.setTargetBeanName(targetBeanName);
}
Set the name of the bean that is to be scoped. |