Browse Source

Fixed AbstractAutoProxyCreator to accept null bean names again

Issue: SPR-10108
pull/208/head
Juergen Hoeller 13 years ago
parent
commit
047db8cdf8
  1. 31
      spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java
  2. 45
      spring-context/src/test/java/org/springframework/aop/config/AopNamespaceHandlerTests.java

31
spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java

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

45
spring-context/src/test/java/org/springframework/aop/config/AopNamespaceHandlerTests.java

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

Loading…
Cancel
Save