Fixed AbstractAutoProxyCreator to accept null bean names again

Issue: SPR-10108
This commit is contained in:
Juergen Hoeller 2012-12-20 17:33:26 +01:00
parent d3da2edf18
commit 047db8cdf8
2 changed files with 50 additions and 26 deletions

View File

@ -268,7 +268,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyConfig
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);
if (!this.targetSourcedBeans.containsKey(beanName)) {
if (beanName == null || !this.targetSourcedBeans.containsKey(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
@ -281,13 +281,15 @@ public abstract class AbstractAutoProxyCreator extends ProxyConfig
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
this.targetSourcedBeans.put(beanName, Boolean.TRUE);
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
if (beanName != null) {
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
this.targetSourcedBeans.put(beanName, Boolean.TRUE);
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
}
return null;
@ -341,7 +343,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyConfig
* @return a proxy wrapping the bean, or the raw bean instance as-is
*/
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (this.targetSourcedBeans.containsKey(beanName)) {
if (beanName != null && this.targetSourcedBeans.containsKey(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
@ -368,17 +370,18 @@ public abstract class AbstractAutoProxyCreator extends ProxyConfig
/**
* Return whether the given bean class represents an infrastructure class
* that should never be proxied.
* <p>Default implementation considers Advisors, Advices and
* AbstractAutoProxyCreators as infrastructure classes.
* <p>The default implementation considers Advices, Advisors and
* AopInfrastructureBeans as infrastructure classes.
* @param beanClass the class of the bean
* @return whether the bean represents an infrastructure class
* @see org.aopalliance.aop.Advice
* @see org.springframework.aop.Advisor
* @see org.aopalliance.intercept.MethodInterceptor
* @see org.springframework.aop.framework.AopInfrastructureBean
* @see #shouldSkip
*/
protected boolean isInfrastructureClass(Class<?> beanClass) {
boolean retVal = Advisor.class.isAssignableFrom(beanClass) ||
Advice.class.isAssignableFrom(beanClass) ||
boolean retVal = Advice.class.isAssignableFrom(beanClass) ||
Advisor.class.isAssignableFrom(beanClass) ||
AopInfrastructureBean.class.isAssignableFrom(beanClass);
if (retVal && logger.isTraceEnabled()) {
logger.trace("Did not attempt to auto-proxy infrastructure class [" + beanClass.getName() + "]");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,19 +16,20 @@
package org.springframework.aop.config;
import static org.junit.Assert.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.junit.Before;
import org.junit.Test;
import test.advice.CountingBeforeAdvice;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.advice.CountingBeforeAdvice;
import static org.junit.Assert.*;
/**
* Unit tests for aop namespace.
@ -44,9 +45,14 @@ public class AopNamespaceHandlerTests {
@Before
public void setUp() {
this.context =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
}
protected ITestBean getTestBean() {
return (ITestBean) this.context.getBean("testBean");
}
@Test
public void testIsProxy() throws Exception {
ITestBean bean = getTestBean();
@ -83,28 +89,43 @@ public class AopNamespaceHandlerTests {
@Test
public void testAspectApplied() throws Exception {
ITestBean testBean = getTestBean();
ITestBean bean = getTestBean();
CountingAspectJAdvice advice = (CountingAspectJAdvice) this.context.getBean("countingAdvice");
assertEquals("Incorrect before count", 0, advice.getBeforeCount());
assertEquals("Incorrect after count", 0, advice.getAfterCount());
testBean.setName("Sally");
bean.setName("Sally");
assertEquals("Incorrect before count", 1, advice.getBeforeCount());
assertEquals("Incorrect after count", 1, advice.getAfterCount());
testBean.getName();
bean.getName();
assertEquals("Incorrect before count", 1, advice.getBeforeCount());
assertEquals("Incorrect after count", 1, advice.getAfterCount());
}
protected ITestBean getTestBean() {
return (ITestBean) this.context.getBean("testBean");
}
@Test
public void testAspectAppliedForInitializeBean() {
ITestBean bean = (ITestBean) this.context.getAutowireCapableBeanFactory().initializeBean(new TestBean(), null);
CountingAspectJAdvice advice = (CountingAspectJAdvice) this.context.getBean("countingAdvice");
assertEquals("Incorrect before count", 0, advice.getBeforeCount());
assertEquals("Incorrect after count", 0, advice.getAfterCount());
bean.setName("Sally");
assertEquals("Incorrect before count", 1, advice.getBeforeCount());
assertEquals("Incorrect after count", 1, advice.getAfterCount());
bean.getName();
assertEquals("Incorrect before count", 1, advice.getBeforeCount());
assertEquals("Incorrect after count", 1, advice.getAfterCount());
}
}
@ -152,5 +173,5 @@ class CountingAspectJAdvice {
public int getAroundCount() {
return this.aroundCount;
}
}
}