|
21 | 21 |
|
22 | 22 | import org.junit.After; |
23 | 23 | import org.junit.Test; |
| 24 | +import org.springframework.beans.BeansException; |
24 | 25 | import org.springframework.beans.factory.BeanCreationException; |
| 26 | +import org.springframework.beans.factory.FactoryBean; |
| 27 | +import org.springframework.beans.factory.InitializingBean; |
25 | 28 | import org.springframework.beans.factory.annotation.Value; |
| 29 | +import org.springframework.beans.factory.support.AbstractBeanDefinition; |
| 30 | +import org.springframework.beans.factory.support.GenericBeanDefinition; |
26 | 31 | import org.springframework.boot.test.EnvironmentTestUtils; |
27 | 32 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
28 | 33 | import org.springframework.context.annotation.Bean; |
|
36 | 41 |
|
37 | 42 | import static org.hamcrest.Matchers.equalTo; |
38 | 43 | import static org.junit.Assert.assertEquals; |
| 44 | +import static org.junit.Assert.assertFalse; |
39 | 45 | import static org.junit.Assert.assertNotNull; |
40 | 46 | import static org.junit.Assert.assertThat; |
| 47 | +import static org.junit.Assert.assertTrue; |
41 | 48 | import static org.junit.Assert.fail; |
42 | 49 |
|
43 | 50 | /** |
@@ -141,6 +148,26 @@ public void testValueBindingForDefaults() throws Exception { |
141 | 148 | equalTo("foo")); |
142 | 149 | } |
143 | 150 |
|
| 151 | + @Test |
| 152 | + public void configurationPropertiesWithFactoryBean() throws Exception { |
| 153 | + ConfigurationPropertiesWithFactoryBean.factoryBeanInit = false; |
| 154 | + this.context = new AnnotationConfigApplicationContext() { |
| 155 | + @Override |
| 156 | + protected void onRefresh() throws BeansException { |
| 157 | + assertFalse("Init too early", |
| 158 | + ConfigurationPropertiesWithFactoryBean.factoryBeanInit); |
| 159 | + super.onRefresh(); |
| 160 | + } |
| 161 | + }; |
| 162 | + this.context.register(ConfigurationPropertiesWithFactoryBean.class); |
| 163 | + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); |
| 164 | + beanDefinition.setBeanClass(FactoryBeanTester.class); |
| 165 | + beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); |
| 166 | + this.context.registerBeanDefinition("test", beanDefinition); |
| 167 | + this.context.refresh(); |
| 168 | + assertTrue("No init", ConfigurationPropertiesWithFactoryBean.factoryBeanInit); |
| 169 | + } |
| 170 | + |
144 | 171 | @Configuration |
145 | 172 | @EnableConfigurationProperties |
146 | 173 | public static class TestConfigurationWithValidatingSetter { |
@@ -299,4 +326,37 @@ public static PropertySourcesPlaceholderConfigurer configurer() { |
299 | 326 |
|
300 | 327 | } |
301 | 328 |
|
| 329 | + @Configuration |
| 330 | + @EnableConfigurationProperties |
| 331 | + public static class ConfigurationPropertiesWithFactoryBean { |
| 332 | + |
| 333 | + public static boolean factoryBeanInit; |
| 334 | + |
| 335 | + } |
| 336 | + |
| 337 | + @SuppressWarnings("rawtypes") |
| 338 | + // Must be a raw type |
| 339 | + static class FactoryBeanTester implements FactoryBean, InitializingBean { |
| 340 | + |
| 341 | + @Override |
| 342 | + public Object getObject() throws Exception { |
| 343 | + return Object.class; |
| 344 | + } |
| 345 | + |
| 346 | + @Override |
| 347 | + public Class<?> getObjectType() { |
| 348 | + return null; |
| 349 | + } |
| 350 | + |
| 351 | + @Override |
| 352 | + public boolean isSingleton() { |
| 353 | + return true; |
| 354 | + } |
| 355 | + |
| 356 | + @Override |
| 357 | + public void afterPropertiesSet() throws Exception { |
| 358 | + ConfigurationPropertiesWithFactoryBean.factoryBeanInit = true; |
| 359 | + } |
| 360 | + |
| 361 | + } |
302 | 362 | } |
0 commit comments