Example usage for org.springframework.core.env ConfigurableEnvironment getPropertySources

List of usage examples for org.springframework.core.env ConfigurableEnvironment getPropertySources

Introduction

In this page you can find the example usage for org.springframework.core.env ConfigurableEnvironment getPropertySources.

Prototype

MutablePropertySources getPropertySources();

Source Link

Document

Return the PropertySources for this Environment in mutable form, allowing for manipulation of the set of PropertySource objects that should be searched when resolving properties against this Environment object.

Usage

From source file:com.pavikumbhar.javaheart.springconfiguration.ContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    ConfigurableEnvironment environment = applicationContext.getEnvironment();
    System.out.println("com.pavikumbhar.javaheart.ContextInitializer.initialize()");
    try {/*w w w. j a  va 2  s  .com*/
        environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties"));
        String profile = environment.getProperty("spring.profiles.active");
        environment.setActiveProfiles(profile);
        System.err.println(" spring.profiles.active profile :" + profile);
        System.err.println("env.properties loaded :" + ContextInitializer.class.getName());
    } catch (IOException e) {
        // it's ok if the file is not there. we will just log that info.
        System.out.println(
                "didn't find env.properties in classpath so not loading it in the AppContextInitialized");
    }
}

From source file:io.gravitee.repository.jdbc.config.DataSourceFactory.java

public Map<String, Object> getAllProperties(ConfigurableEnvironment aEnv) {
    Map<String, Object> result = new HashMap<>();
    aEnv.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
    return result;
}

From source file:org.carewebframework.api.spring.FrameworkAppContext.java

/**
 * Constructor for creating an application context. Disallows bean overrides by default.
 * //from w ww . j  a  va 2s. co m
 * @param testConfig If true, use test profiles. If false, use production profiles.
 * @param locations Optional list of configuration file locations. If not specified, defaults to
 *            the default configuration locations ({@link #getDefaultConfigLocations}).
 */
public FrameworkAppContext(boolean testConfig, String... locations) {
    super();
    setAllowBeanDefinitionOverriding(false);
    setConfigLocations(locations == null || locations.length == 0 ? null : locations);
    ConfigurableEnvironment env = getEnvironment();
    env.setActiveProfiles(testConfig ? Constants.PROFILES_TEST : Constants.PROFILES_PROD);
    env.getPropertySources().addLast(new DomainPropertySource(this));
}

From source file:edu.jhuapl.openessence.config.AppInitializer.java

private void addPropertySources(ConfigurableWebApplicationContext ctx) {
    ConfigurableEnvironment env = ctx.getEnvironment();

    // add properties that don't come from .properties files
    env.getPropertySources().addFirst(getBuiltinPropertySource(ctx));

    try {/*ww w . ja v a  2 s  . c  om*/
        Resource[] classpathPropResources = ctx.getResources("classpath:/config/*.properties");
        for (PropertySource<?> p : getPropertySources(Arrays.asList(classpathPropResources))) {
            env.getPropertySources().addFirst(p);
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:net.ggtools.maven.DDLGeneratorMojo.java

AnnotationConfigApplicationContext createApplicationContext() {
    final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    final ConfigurableEnvironment environment = applicationContext.getEnvironment();
    final MutablePropertySources propertySources = environment.getPropertySources();
    final MapPropertySource propertySource = createPropertySource();
    propertySources.addFirst(propertySource);
    applicationContext.register(SpringConfiguration.class);
    applicationContext.refresh();/*from   w  w  w . j a  va 2  s.  c om*/
    return applicationContext;
}

From source file:io.pivotal.springcloud.ssl.CloudFoundryCertificateTruster.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {

    if (!inited) {
        inited = true;//from   w  ww  .  j a  v  a  2  s  .c o  m
        String trustCertUrls = null;
        String trustStore = null;
        String trustStorePassword = null;

        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        for (PropertySource<?> propertySource : environment.getPropertySources()) {
            if (propertySource.containsProperty("app.ssl.trustStore"))
                trustStore = (String) propertySource.getProperty("app.ssl.trustStore");
            if (propertySource.containsProperty("app.ssl.trustStorePassword"))
                trustStorePassword = (String) propertySource.getProperty("app.ssl.trustStorePassword");
            if (propertySource.containsProperty("app.ssl.trustCertUrls"))
                trustCertUrls = (String) propertySource.getProperty("app.ssl.trustCertUrls");
        }

        if (trustCertUrls != null)
            trustCertificatesFromURLInternal(trustCertUrls);

        if (trustStore != null)
            trustCertificatesFromStoreInternal(applicationContext, trustStore, trustStorePassword);
    }
}

From source file:io.bitsquare.app.BitsquareEnvironmentTests.java

@Test
public void testPropertySourcePrecedence() {
    PropertySource commandlineProps = new MockPropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)
            .withProperty("key.x", "x.commandline");

    PropertySource filesystemProps = new MockPropertySource(BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME)
            .withProperty("key.x", "x.env").withProperty("key.y", "y.env");

    ConfigurableEnvironment env = new BitsquareEnvironment(commandlineProps) {
        @Override//from  w  w  w.  j  a  va 2s  .  c  o m
        PropertySource<?> filesystemProperties() {
            return filesystemProps;
        }
    };
    MutablePropertySources propertySources = env.getPropertySources();

    assertThat(propertySources.precedenceOf(named(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)), equalTo(0));
    assertThat(propertySources.precedenceOf(named(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(1));
    assertThat(propertySources.precedenceOf(named(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(2));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_FILESYSTEM_PROPERTY_SOURCE_NAME)), equalTo(3));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(4));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(5));
    assertThat(propertySources.size(), equalTo(6));

    assertThat(env.getProperty("key.x"), equalTo("x.commandline")); // commandline value wins due to precedence
    assertThat(env.getProperty("key.y"), equalTo("y.env")); // env value wins because it's the only one available
}

From source file:com.indeed.imhotep.web.config.PropertiesInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    final ConfigurableEnvironment springEnv = applicationContext.getEnvironment();

    activateSpringProfiles(springEnv);//from w w  w.  j a  va 2  s . c  om

    final MutablePropertySources propSources = springEnv.getPropertySources();

    for (String location : getPropertyLocations(applicationContext)) {
        tryAddPropertySource(applicationContext, propSources, location);
    }

    addPropertySources(applicationContext, propSources);
}

From source file:com.github.dactiv.fear.commons.Apis.java

@Override
public void afterPropertiesSet() throws Exception {

    PropertySourcesPlaceholderConfigurer configurer = null;
    PropertySources propertySources = null;
    try {// ww  w. j  a  v  a2 s .c  om
        configurer = applicationContext.getBean(PropertySourcesPlaceholderConfigurer.class);
        propertySources = configurer.getAppliedPropertySources();
    } catch (Exception e) {
        LOGGER.warn("install " + PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME
                + " error", e);
    }

    if (propertySources == null) {
        return;
    }

    Field field = ReflectionUtils.findField(configurer.getClass(), "environment");
    field.setAccessible(Boolean.TRUE);
    environment = (Environment) field.get(configurer);
    if (environment instanceof ConfigurableEnvironment) {
        ConfigurableEnvironment ce = (ConfigurableEnvironment) environment;
        MutablePropertySources sources = ce.getPropertySources();
        PropertySource<?> ps = propertySources
                .get(PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME);
        sources.addFirst(ps);
    }
}

From source file:net.community.chest.gitcloud.facade.AbstractContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    ConfigurableEnvironment environment = applicationContext.getEnvironment();
    MutablePropertySources propSources = environment.getPropertySources();
    ExtendedPlaceholderResolver sourcesResolver = ExtendedPlaceholderResolverUtils
            .toPlaceholderResolver(propSources);
    File appBase = resolveApplicationBase(propSources, sourcesResolver);
    File configFile = getApplicationConfigFile(appBase, sourcesResolver);
    Collection<String> activeProfiles = resolveActiveProfiles(sourcesResolver);
    if (ExtendedCollectionUtils.size(activeProfiles) > 0) {
        environment.setActiveProfiles(activeProfiles.toArray(new String[activeProfiles.size()]));
    }/*from  w  w w. j  a  va2 s .com*/

    try {
        ensureFoldersExistence(appBase, configFile, sourcesResolver);
    } catch (IOException e) {
        logger.error("ensureFoldersExistence(" + ExtendedFileUtils.toString(appBase) + ")" + " "
                + e.getClass().getSimpleName() + ": " + e.getMessage());
    }

    if (logger.isDebugEnabled()) {
        showArtifactsVersions();
    }
}