LazyInitTargetSource works for @Bean targets as well
Issue: SPR-10508 Issue: SPR-8080
This commit is contained in:
parent
b64f680f19
commit
8c9274e017
|
@ -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 {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Exception>();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<ApplicationContextEvent> {
|
||||
|
||||
@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<ApplicationContextEvent> {
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue