if (!(beanFactory instanceof ConfigurableBeanFactory))
{
throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
}
ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) beanFactory;
ScopedBeanTargetSource scopedTargetSource = new ScopedBeanTargetSource(
scope, conversationName, targetBeanName, objectFactory, beanFactory);
ProxyFactory pf = new ProxyFactory();
pf.setProxyTargetClass(true);
pf.setTargetSource(scopedTargetSource);
Class beanType = beanFactory.getType(targetBeanName);
if (beanType == null) {
throw new IllegalStateException("Cannot create scoped proxy for bean '" + targetBeanName +
"': Target type could not be determined at the time of proxy creation.");
}
// Add an introduction that implements only the methods on ScopedObject.
// Not sure if this is useful...
ScopedObject scopedObject = new DefaultScopedObject(cbf, 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.
// Not sure if this is needed....
pf.addInterface(AopInfrastructureBean.class);
return pf.getProxy(cbf.getBeanClassLoader());
}