|
| 1 | +/* |
| 2 | + * Copyright 2012-2013 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.boot; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.context.ApplicationContext; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.context.annotation.Primary; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Dave Syer |
| 30 | + */ |
| 31 | +public class OverrideSourcesTests { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void beanInjectedToMainConfiguration() { |
| 35 | + ApplicationContext context = SpringApplication.run( |
| 36 | + new Object[] { MainConfiguration.class }, |
| 37 | + new String[] { "--spring.main.web_environment=false" }); |
| 38 | + assertEquals("foo", context.getBean(Service.class).bean.name); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void primaryBeanInjectedProvingSourcesNotOverridden() { |
| 43 | + ApplicationContext context = SpringApplication |
| 44 | + .run(new Object[] { MainConfiguration.class, TestConfiguration.class }, |
| 45 | + new String[] { "--spring.main.web_environment=false", |
| 46 | + "--spring.main.sources=org.springframework.boot.OverrideSourcesTests.MainConfiguration" }); |
| 47 | + assertEquals("bar", context.getBean(Service.class).bean.name); |
| 48 | + } |
| 49 | + |
| 50 | + @Configuration |
| 51 | + protected static class TestConfiguration { |
| 52 | + |
| 53 | + @Bean |
| 54 | + @Primary |
| 55 | + public TestBean another() { |
| 56 | + return new TestBean("bar"); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Configuration |
| 62 | + protected static class MainConfiguration { |
| 63 | + |
| 64 | + @Bean |
| 65 | + public TestBean first() { |
| 66 | + return new TestBean("foo"); |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + public Service Service() { |
| 71 | + return new Service(); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + protected static class Service { |
| 77 | + @Autowired |
| 78 | + private TestBean bean; |
| 79 | + } |
| 80 | + |
| 81 | + protected static class TestBean { |
| 82 | + |
| 83 | + private String name; |
| 84 | + |
| 85 | + public TestBean(String name) { |
| 86 | + this.name = name; |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | +} |
0 commit comments