|
| 1 | +/* |
| 2 | + * Copyright 2002-2011 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.annotation; |
| 18 | + |
| 19 | +import static org.hamcrest.CoreMatchers.not; |
| 20 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 21 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 22 | +import static org.hamcrest.CoreMatchers.sameInstance; |
| 23 | +import static org.junit.Assert.assertThat; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | +import org.springframework.beans.BeansException; |
| 27 | +import org.springframework.beans.TestBean; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
| 30 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests semantics of declaring {@link BeanFactoryPostProcessor}-returning @Bean |
| 34 | + * methods, specifically as regards static @Bean methods and the avoidance of |
| 35 | + * container lifecycle issues when BFPPs are in the mix. |
| 36 | + * |
| 37 | + * @author Chris Beams |
| 38 | + * @since 3.1 |
| 39 | + */ |
| 40 | +public class ConfigurationClassAndBFPPTests { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void autowiringFailsWithBFPPAsInstanceMethod() { |
| 44 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 45 | + ctx.register(TestBeanConfig.class, AutowiredConfigWithBFPPAsInstanceMethod.class); |
| 46 | + ctx.refresh(); |
| 47 | + // instance method BFPP interferes with lifecycle -> autowiring fails! |
| 48 | + // WARN-level logging should have been issued about returning BFPP from non-static @Bean method |
| 49 | + assertThat(ctx.getBean(AutowiredConfigWithBFPPAsInstanceMethod.class).autowiredTestBean, nullValue()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void autowiringSucceedsWithBFPPAsStaticMethod() { |
| 54 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 55 | + ctx.register(TestBeanConfig.class, AutowiredConfigWithBFPPAsStaticMethod.class); |
| 56 | + ctx.refresh(); |
| 57 | + // static method BFPP does not interfere with lifecycle -> autowiring succeeds |
| 58 | + assertThat(ctx.getBean(AutowiredConfigWithBFPPAsStaticMethod.class).autowiredTestBean, notNullValue()); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Configuration |
| 63 | + static class TestBeanConfig { |
| 64 | + @Bean |
| 65 | + public TestBean testBean() { |
| 66 | + return new TestBean(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @Configuration |
| 72 | + static class AutowiredConfigWithBFPPAsInstanceMethod { |
| 73 | + @Autowired TestBean autowiredTestBean; |
| 74 | + |
| 75 | + @Bean |
| 76 | + public BeanFactoryPostProcessor bfpp() { |
| 77 | + return new BeanFactoryPostProcessor() { |
| 78 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 79 | + // no-op |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + @Configuration |
| 87 | + static class AutowiredConfigWithBFPPAsStaticMethod { |
| 88 | + @Autowired TestBean autowiredTestBean; |
| 89 | + |
| 90 | + @Bean |
| 91 | + public static final BeanFactoryPostProcessor bfpp() { |
| 92 | + return new BeanFactoryPostProcessor() { |
| 93 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 94 | + // no-op |
| 95 | + } |
| 96 | + }; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + @Test |
| 102 | + @SuppressWarnings("static-access") |
| 103 | + public void staticBeanMethodsDoNotRespectScoping() { |
| 104 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 105 | + ctx.register(ConfigWithStaticBeanMethod.class); |
| 106 | + ctx.refresh(); |
| 107 | + |
| 108 | + ConfigWithStaticBeanMethod config = ctx.getBean(ConfigWithStaticBeanMethod.class); |
| 109 | + assertThat(config.testBean(), not(sameInstance(config.testBean()))); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + @Configuration |
| 114 | + static class ConfigWithStaticBeanMethod { |
| 115 | + @Bean |
| 116 | + public static TestBean testBean() { |
| 117 | + return new TestBean("foo"); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +} |
0 commit comments