Search in sources :

Example 1 with PropertySource

use of org.springframework.context.annotation.PropertySource in project chassis by Kixeye.

the class ConfigurationBuilder method initModuleConfiguration.

private void initModuleConfiguration() {
    if (!scanModuleConfigurations) {
        this.moduleDefaultConfiguration = new ConcurrentMapConfiguration();
        return;
    }
    HashMap<String, Object> base = new HashMap<>();
    Set<Class<?>> types = reflections.getTypesAnnotatedWith(PropertySource.class);
    for (Class<?> type : types) {
        PropertySource propertySource = type.getAnnotation(PropertySource.class);
        String[] propertiesFiles = propertySource.value();
        for (String propertyFile : propertiesFiles) {
            Properties properties = new Properties();
            try (InputStream is = resourceLoader.getResource(SystemPropertyUtils.resolvePlaceholders(propertyFile)).getInputStream()) {
                properties.load(is);
                LOGGER.debug("Initializing module properties from path " + propertyFile);
            } catch (Exception e) {
                BootstrapException.resourceLoadingFailed(propertyFile, e);
            }
            join(base, properties, propertyFile, propertiesFiles);
        }
    }
    this.moduleDefaultConfiguration = new ConcurrentMapConfiguration(base);
}
Also used : HashMap(java.util.HashMap) ConcurrentMapConfiguration(com.netflix.config.ConcurrentMapConfiguration) InputStream(java.io.InputStream) Properties(java.util.Properties) BootstrapException(com.kixeye.chassis.bootstrap.BootstrapException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ApplicationConfigurationNotFoundException(com.kixeye.chassis.bootstrap.BootstrapException.ApplicationConfigurationNotFoundException) PropertySource(org.springframework.context.annotation.PropertySource)

Example 2 with PropertySource

use of org.springframework.context.annotation.PropertySource in project spring-boot by spring-projects.

the class ConfigFileApplicationListenerTests method yamlSetsProfiles.

@Test
public void yamlSetsProfiles() throws Exception {
    this.initializer.setSearchNames("testsetprofiles");
    this.initializer.postProcessEnvironment(this.environment, this.application);
    assertThat(this.environment.getActiveProfiles()).containsExactly("dev");
    String property = this.environment.getProperty("my.property");
    assertThat(this.environment.getActiveProfiles()).contains("dev");
    assertThat(property).isEqualTo("fromdevprofile");
    ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment.getPropertySources().get(ConfigFileApplicationListener.APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME);
    Collection<org.springframework.core.env.PropertySource<?>> sources = propertySource.getSource();
    assertThat(sources).hasSize(2);
    List<String> names = new ArrayList<>();
    for (org.springframework.core.env.PropertySource<?> source : sources) {
        if (source instanceof EnumerableCompositePropertySource) {
            for (org.springframework.core.env.PropertySource<?> nested : ((EnumerableCompositePropertySource) source).getSource()) {
                names.add(nested.getName());
            }
        } else {
            names.add(source.getName());
        }
    }
    assertThat(names).contains("applicationConfig: [classpath:/testsetprofiles.yml]#dev", "applicationConfig: [classpath:/testsetprofiles.yml]");
}
Also used : EnumerableCompositePropertySource(org.springframework.boot.env.EnumerableCompositePropertySource) ArrayList(java.util.ArrayList) ConfigurationPropertySources(org.springframework.boot.context.config.ConfigFileApplicationListener.ConfigurationPropertySources) PropertySource(org.springframework.context.annotation.PropertySource) SimpleCommandLinePropertySource(org.springframework.core.env.SimpleCommandLinePropertySource) EnumerableCompositePropertySource(org.springframework.boot.env.EnumerableCompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) Test(org.junit.Test)

Aggregations

PropertySource (org.springframework.context.annotation.PropertySource)2 BootstrapException (com.kixeye.chassis.bootstrap.BootstrapException)1 ApplicationConfigurationNotFoundException (com.kixeye.chassis.bootstrap.BootstrapException.ApplicationConfigurationNotFoundException)1 ConcurrentMapConfiguration (com.netflix.config.ConcurrentMapConfiguration)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 Test (org.junit.Test)1 ConfigurationPropertySources (org.springframework.boot.context.config.ConfigFileApplicationListener.ConfigurationPropertySources)1 EnumerableCompositePropertySource (org.springframework.boot.env.EnumerableCompositePropertySource)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 SimpleCommandLinePropertySource (org.springframework.core.env.SimpleCommandLinePropertySource)1