Use concrete types in EnviromentRepository bean definitions

so that @OnMissingBeanCondition has a chance to detect all the
interfaces.
This commit is contained in:
Dave Syer
2016-09-28 17:45:33 -04:00
parent a5386c168b
commit 512e5869e1
5 changed files with 94 additions and 6 deletions

View File

@@ -34,7 +34,7 @@ import org.springframework.context.annotation.Import;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ EnvironmentRepositoryConfiguration.class, ResourceRepositoryConfiguration.class,
@Import({ ResourceRepositoryConfiguration.class, EnvironmentRepositoryConfiguration.class,
ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class })
public @interface EnableConfigServer {

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.config.server.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.cloud.config.server.encryption.EnvironmentEncryptor;
import org.springframework.cloud.config.server.environment.EnvironmentController;
@@ -42,7 +43,7 @@ public class ConfigServerMvcConfiguration extends WebMvcConfigurerAdapter {
@Autowired
private EnvironmentRepository repository;
@Autowired
@Autowired(required=false)
private ResourceRepository resources;
@Autowired
@@ -69,6 +70,7 @@ public class ConfigServerMvcConfiguration extends WebMvcConfigurerAdapter {
}
@Bean
@ConditionalOnBean(ResourceRepository.class)
public ResourceController resourceController() {
ResourceController controller = new ResourceController(this.resources,
encrypted());

View File

@@ -51,7 +51,7 @@ public class EnvironmentRepositoryConfiguration {
private ConfigurableEnvironment environment;
@Bean
public EnvironmentRepository environmentRepository() {
public NativeEnvironmentRepository environmentRepository() {
return new NativeEnvironmentRepository(this.environment);
}
@@ -68,7 +68,7 @@ public class EnvironmentRepositoryConfiguration {
private ConfigServerProperties server;
@Bean
public EnvironmentRepository environmentRepository() {
public MultipleJGitEnvironmentRepository environmentRepository() {
MultipleJGitEnvironmentRepository repository = new MultipleJGitEnvironmentRepository(this.environment);
if (this.server.getDefaultLabel()!=null) {
repository.setDefaultLabel(this.server.getDefaultLabel());
@@ -87,7 +87,7 @@ public class EnvironmentRepositoryConfiguration {
private ConfigServerProperties server;
@Bean
public EnvironmentRepository environmentRepository() {
public SvnKitEnvironmentRepository environmentRepository() {
SvnKitEnvironmentRepository repository = new SvnKitEnvironmentRepository(this.environment);
if (this.server.getDefaultLabel()!=null) {
repository.setDefaultLabel(this.server.getDefaultLabel());

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.cloud.config.server.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.config.server.environment.SearchPathLocator;
@@ -28,11 +29,12 @@ import org.springframework.context.annotation.Configuration;
*
*/
@Configuration
@ConditionalOnMissingBean(ResourceRepository.class)
@EnableConfigurationProperties(ConfigServerProperties.class)
@ConditionalOnMissingBean(ResourceRepository.class)
public class ResourceRepositoryConfiguration {
@Bean
@ConditionalOnBean(SearchPathLocator.class)
public ResourceRepository resourceRepository(SearchPathLocator service) {
return new GenericResourceRepository(service);
}

View File

@@ -0,0 +1,84 @@
/*
* Copyright 2012-2015 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.cloud.config.server.config;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.config.server.config.CustomEnvironmentRepositoryTests.TestApplication;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.assertFalse;
/**
* @author Dave Syer
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class)
@IntegrationTest({"server.port:0", "spring.config.name:configserver"})
@WebAppConfiguration
@ActiveProfiles("test")
@DirtiesContext
public class CustomEnvironmentRepositoryTests {
@Value("${local.server.port}")
private int port;
@Test
public void contextLoads() {
Environment environment = new TestRestTemplate().getForObject("http://localhost:"
+ port + "/foo/development/", Environment.class);
assertFalse(environment.getPropertySources().isEmpty());
}
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
protected static class TestApplication {
@Bean
public EnvironmentRepository environmentRepository() {
return new EnvironmentRepository() {
@Override
public Environment findOne(String application, String profile, String label) {
return new Environment("test", new String[0], "label", "version");
}
};
}
public static void main(String[] args) throws Exception {
SpringApplication.run(CustomEnvironmentRepositoryTests.TestApplication.class, args);
}
}
}