Commit 9a7a4a46 authored by Phillip Webb's avatar Phillip Webb

Don't cause early FactoryBean instantiation

Update ConfigurationPropertiesBindingPostProcessor to use
`getBeansOfType` with `allowEagerInit=false` rather than `getBean`.

This prevents FactoryBeans from being instantiated early when their
type is not known.

Fixed gh-1365
parent cac3865f
......@@ -18,6 +18,7 @@ package org.springframework.boot.context.properties;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
......@@ -203,15 +204,10 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
}
private PropertySources deducePropertySources() {
try {
PropertySourcesPlaceholderConfigurer configurer = this.beanFactory
.getBean(PropertySourcesPlaceholderConfigurer.class);
PropertySources propertySources = configurer.getAppliedPropertySources();
PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
if (configurer != null) {
// Flatten the sources into a single list so they can be iterated
return new FlatPropertySources(propertySources);
}
catch (NoSuchBeanDefinitionException ex) {
// Continue if no PropertySourcesPlaceholderConfigurer bean
return new FlatPropertySources(configurer.getAppliedPropertySources());
}
if (this.environment instanceof ConfigurableEnvironment) {
......@@ -224,6 +220,20 @@ public class ConfigurationPropertiesBindingPostProcessor implements BeanPostProc
return new MutablePropertySources();
}
private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholderConfigurer() {
// Take care not to cause early instantiation of all FactoryBeans
if (this.beanFactory instanceof ListableBeanFactory) {
ListableBeanFactory listableBeanFactory = (ListableBeanFactory) this.beanFactory;
Map<String, PropertySourcesPlaceholderConfigurer> beans = listableBeanFactory
.getBeansOfType(PropertySourcesPlaceholderConfigurer.class, false,
false);
if (beans.size() == 1) {
return beans.values().iterator().next();
}
}
return null;
}
private <T> T getOptionalBean(String name, Class<T> type) {
try {
return this.beanFactory.getBean(name, type);
......
......@@ -21,8 +21,13 @@ import javax.validation.constraints.NotNull;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
......@@ -36,8 +41,10 @@ import org.springframework.validation.Validator;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
......@@ -141,6 +148,26 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
equalTo("foo"));
}
@Test
public void configurationPropertiesWithFactoryBean() throws Exception {
ConfigurationPropertiesWithFactoryBean.factoryBeanInit = false;
this.context = new AnnotationConfigApplicationContext() {
@Override
protected void onRefresh() throws BeansException {
assertFalse("Init too early",
ConfigurationPropertiesWithFactoryBean.factoryBeanInit);
super.onRefresh();
}
};
this.context.register(ConfigurationPropertiesWithFactoryBean.class);
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(FactoryBeanTester.class);
beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
this.context.registerBeanDefinition("test", beanDefinition);
this.context.refresh();
assertTrue("No init", ConfigurationPropertiesWithFactoryBean.factoryBeanInit);
}
@Configuration
@EnableConfigurationProperties
public static class TestConfigurationWithValidatingSetter {
......@@ -299,4 +326,37 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
}
@Configuration
@EnableConfigurationProperties
public static class ConfigurationPropertiesWithFactoryBean {
public static boolean factoryBeanInit;
}
@SuppressWarnings("rawtypes")
// Must be a raw type
static class FactoryBeanTester implements FactoryBean, InitializingBean {
@Override
public Object getObject() throws Exception {
return Object.class;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void afterPropertiesSet() throws Exception {
ConfigurationPropertiesWithFactoryBean.factoryBeanInit = true;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment