diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index d148d4447f..29987b591b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -372,6 +372,10 @@ class ConstructorResolver { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "factory-bean '" + factoryBeanName + "' (or a BeanPostProcessor involved) returned null"); } + if (mbd.isSingleton() && this.beanFactory.containsSingleton(beanName)) { + throw new IllegalStateException("About-to-be-created singleton instance implicitly appeared " + + "through the creation of the factory bean that its bean definition points to"); + } factoryClass = factoryBean.getClass(); isStatic = false; } @@ -904,4 +908,5 @@ class ConstructorResolver { } } } + } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java index 77df1a21b2..73441b9f33 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java @@ -220,12 +220,22 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements logger.debug("Creating shared instance of singleton bean '" + beanName + "'"); } beforeSingletonCreation(beanName); + boolean newSingleton = false; boolean recordSuppressedExceptions = (this.suppressedExceptions == null); if (recordSuppressedExceptions) { this.suppressedExceptions = new LinkedHashSet(); } try { singletonObject = singletonFactory.getObject(); + newSingleton = true; + } + catch (IllegalStateException ex) { + // Has the singleton object implicitly appeared in the meantime -> + // if yes, proceed with it since the exception indicates that state. + singletonObject = this.singletonObjects.get(beanName); + if (singletonObject == null) { + throw ex; + } } catch (BeanCreationException ex) { if (recordSuppressedExceptions) { @@ -241,7 +251,9 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements } afterSingletonCreation(beanName); } - addSingleton(beanName, singletonObject); + if (newSingleton) { + addSingleton(beanName, singletonObject); + } } return (singletonObject != NULL_OBJECT ? singletonObject : null); } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java index a9025dc94d..f7255b011a 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java @@ -296,7 +296,7 @@ class ConfigurationClassEnhancer { } } - if (isCurrentlyInvokedFactoryMethod(beanMethod) && !beanFactory.containsSingleton(beanName)) { + if (isCurrentlyInvokedFactoryMethod(beanMethod)) { // The factory is calling the bean method in order to instantiate and register the bean // (i.e. via a getBean() call) -> invoke the super implementation of the method to actually // create the bean instance. @@ -306,7 +306,7 @@ class ConfigurationClassEnhancer { "result in a failure to process annotations such as @Autowired, " + "@Resource and @PostConstruct within the method's declaring " + "@Configuration class. Add the 'static' modifier to this method to avoid " + - "these container lifecycle issues; see @Bean Javadoc for complete details", + "these container lifecycle issues; see @Bean javadoc for complete details", beanMethod.getDeclaringClass().getSimpleName(), beanMethod.getName())); } return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs); diff --git a/spring-context/src/test/java/org/springframework/context/annotation/AutoProxyLazyInitTests.java b/spring-context/src/test/java/org/springframework/context/annotation/AutoProxyLazyInitTests.java new file mode 100644 index 0000000000..a6042b693a --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/AutoProxyLazyInitTests.java @@ -0,0 +1,223 @@ +/* + * Copyright 2002-2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.context.annotation; + +import javax.annotation.PreDestroy; + +import org.junit.Test; + +import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; +import org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator; +import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationContextEvent; + +import static org.junit.Assert.*; + +/** + * @author Juergen Hoeller + * @author Arrault Fabien + */ +public class AutoProxyLazyInitTests { + + @Test + public void withStaticBeanMethod() { + MyBeanImpl.initialized = false; + + ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class); + MyBean bean = ctx.getBean("myBean", MyBean.class); + + assertFalse(MyBeanImpl.initialized); + bean.doIt(); + assertTrue(MyBeanImpl.initialized); + } + + @Test + public void withStaticBeanMethodAndInterface() { + MyBeanImpl.initialized = false; + + ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class); + MyBean bean = ctx.getBean("myBean", MyBean.class); + + assertFalse(MyBeanImpl.initialized); + bean.doIt(); + assertTrue(MyBeanImpl.initialized); + } + + @Test + public void withNonStaticBeanMethod() { + MyBeanImpl.initialized = false; + + ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class); + MyBean bean = ctx.getBean("myBean", MyBean.class); + + assertFalse(MyBeanImpl.initialized); + bean.doIt(); + assertTrue(MyBeanImpl.initialized); + } + + @Test + public void withNonStaticBeanMethodAndInterface() { + MyBeanImpl.initialized = false; + + ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class); + MyBean bean = ctx.getBean("myBean", MyBean.class); + + assertFalse(MyBeanImpl.initialized); + bean.doIt(); + assertTrue(MyBeanImpl.initialized); + } + + + public static interface MyBean { + + public String doIt(); + } + + + public static class MyBeanImpl implements MyBean { + + public static boolean initialized = false; + + public MyBeanImpl() { + initialized = true; + } + + @Override + public String doIt() { + return "From implementation"; + } + + @PreDestroy + public void destroy() { + } + } + + + @Configuration + public static class ConfigWithStatic { + + @Bean + public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { + BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator(); + autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator()); + return autoProxyCreator; + } + + @Bean + public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { + return new StrictLazyInitTargetSourceCreator(); + } + + @Bean + @Lazy + public static MyBean myBean() { + return new MyBeanImpl(); + } + } + + + @Configuration + public static class ConfigWithStaticAndInterface implements ApplicationListener { + + @Bean + public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { + BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator(); + autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator()); + return autoProxyCreator; + } + + @Bean + public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { + return new StrictLazyInitTargetSourceCreator(); + } + + @Bean + @Lazy + public static MyBean myBean() { + return new MyBeanImpl(); + } + + @Override + public void onApplicationEvent(ApplicationContextEvent event) { + } + } + + + @Configuration + public static class ConfigWithNonStatic { + + @Bean + public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { + BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator(); + autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator()); + return autoProxyCreator; + } + + @Bean + public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { + return new StrictLazyInitTargetSourceCreator(); + } + + @Bean + @Lazy + public MyBean myBean() { + return new MyBeanImpl(); + } + } + + + @Configuration + public static class ConfigWithNonStaticAndInterface implements ApplicationListener { + + @Bean + public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() { + BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator(); + autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator()); + return autoProxyCreator; + } + + @Bean + public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() { + return new StrictLazyInitTargetSourceCreator(); + } + + @Bean + @Lazy + public MyBean myBean() { + return new MyBeanImpl(); + } + + @Override + public void onApplicationEvent(ApplicationContextEvent event) { + } + } + + + private static class StrictLazyInitTargetSourceCreator extends LazyInitTargetSourceCreator { + + @Override + protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(Class beanClass, String beanName) { + if ("myBean".equals(beanName)) { + assertEquals(MyBean.class, beanClass); + } + return super.createBeanFactoryBasedTargetSource(beanClass, beanName); + } + } + +}