From 047db8cdf8c7ee0b937c19a6ba09528d209af786 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 20 Dec 2012 17:33:26 +0100 Subject: [PATCH] Fixed AbstractAutoProxyCreator to accept null bean names again Issue: SPR-10108 --- .../autoproxy/AbstractAutoProxyCreator.java | 31 +++++++------ .../aop/config/AopNamespaceHandlerTests.java | 45 ++++++++++++++----- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java index 3216c3e101f..4e4f6380086 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java +++ b/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 { 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. - *

Default implementation considers Advisors, Advices and - * AbstractAutoProxyCreators as infrastructure classes. + *

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() + "]"); diff --git a/spring-context/src/test/java/org/springframework/aop/config/AopNamespaceHandlerTests.java b/spring-context/src/test/java/org/springframework/aop/config/AopNamespaceHandlerTests.java index 21dd905a33a..14620bafaa3 100644 --- a/spring-context/src/test/java/org/springframework/aop/config/AopNamespaceHandlerTests.java +++ b/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"); * 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; } -} +}