use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.
the class ProxyFactoryBean method newPrototypeInstance.
/**
* Create a new prototype instance of this class's created proxy object,
* backed by an independent AdvisedSupport configuration.
* @return a totally independent proxy, whose advice we may manipulate in isolation
*/
private synchronized Object newPrototypeInstance() {
// but will have an independent copy.
if (logger.isTraceEnabled()) {
logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this);
}
ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
// The copy needs a fresh advisor chain, and a fresh TargetSource.
TargetSource targetSource = freshTargetSource();
copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
// Rely on AOP infrastructure to tell us what interfaces to proxy.
copy.setInterfaces(ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), this.proxyClassLoader));
}
copy.setFrozen(this.freezeProxy);
if (logger.isTraceEnabled()) {
logger.trace("Using ProxyCreatorSupport copy: " + copy);
}
return getProxy(copy.createAopProxy());
}
use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.
the class AbstractAutoProxyCreator method postProcessBeforeInstantiation.
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);
if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
// The TargetSource will handle target instances in a custom fashion.
if (beanName != null) {
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
this.targetSourcedBeans.add(beanName);
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
}
return null;
}
use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testProxyIsBoundBeforeTargetSourceInvoked.
@Test
public void testProxyIsBoundBeforeTargetSourceInvoked() {
final TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new DebugInterceptor());
pf.setExposeProxy(true);
final ITestBean proxy = (ITestBean) createProxy(pf);
Advised config = (Advised) proxy;
// This class just checks proxy is bound before getTarget() call
config.setTargetSource(new TargetSource() {
@Override
public Class<?> getTargetClass() {
return TestBean.class;
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() throws Exception {
assertEquals(proxy, AopContext.currentProxy());
return target;
}
@Override
public void releaseTarget(Object target) throws Exception {
}
});
// Just test anything: it will fail if context wasn't found
assertEquals(0, proxy.getAge());
}
Aggregations