Search in sources :

Example 11 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class CloudPlatformTests method getActiveWhenNotInCloudShouldReturnNull.

@Test
public void getActiveWhenNotInCloudShouldReturnNull() throws Exception {
    Environment environment = new MockEnvironment();
    CloudPlatform platform = CloudPlatform.getActive(environment);
    assertThat(platform).isNull();
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) Environment(org.springframework.core.env.Environment) MockEnvironment(org.springframework.mock.env.MockEnvironment) Test(org.junit.Test)

Example 12 with Environment

use of org.springframework.core.env.Environment in project spring-boot by spring-projects.

the class EnvironmentEndpoint method getPropertySources.

private MutablePropertySources getPropertySources() {
    MutablePropertySources sources;
    Environment environment = getEnvironment();
    if (environment != null && environment instanceof ConfigurableEnvironment) {
        sources = ((ConfigurableEnvironment) environment).getPropertySources();
    } else {
        sources = new StandardEnvironment().getPropertySources();
    }
    return sources;
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Environment(org.springframework.core.env.Environment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) StandardEnvironment(org.springframework.core.env.StandardEnvironment)

Example 13 with Environment

use of org.springframework.core.env.Environment in project spring-framework by spring-projects.

the class PropertySourcesPlaceholderConfigurer method postProcessBeanFactory.

/**
	 * {@inheritDoc}
	 * <p>Processing occurs by replacing ${...} placeholders in bean definitions by resolving each
	 * against this configurer's set of {@link PropertySources}, which includes:
	 * <ul>
	 * <li>all {@linkplain org.springframework.core.env.ConfigurableEnvironment#getPropertySources
	 * environment property sources}, if an {@code Environment} {@linkplain #setEnvironment is present}
	 * <li>{@linkplain #mergeProperties merged local properties}, if {@linkplain #setLocation any}
	 * {@linkplain #setLocations have} {@linkplain #setProperties been}
	 * {@linkplain #setPropertiesArray specified}
	 * <li>any property sources set by calling {@link #setPropertySources}
	 * </ul>
	 * <p>If {@link #setPropertySources} is called, <strong>environment and local properties will be
	 * ignored</strong>. This method is designed to give the user fine-grained control over property
	 * sources, and once set, the configurer makes no assumptions about adding additional sources.
	 */
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (this.propertySources == null) {
        this.propertySources = new MutablePropertySources();
        if (this.environment != null) {
            this.propertySources.addLast(new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) {

                @Override
                public String getProperty(String key) {
                    return this.source.getProperty(key);
                }
            });
        }
        try {
            PropertySource<?> localPropertySource = new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties());
            if (this.localOverride) {
                this.propertySources.addFirst(localPropertySource);
            } else {
                this.propertySources.addLast(localPropertySource);
            }
        } catch (IOException ex) {
            throw new BeanInitializationException("Could not load properties", ex);
        }
    }
    processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
    this.appliedPropertySources = this.propertySources;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) Environment(org.springframework.core.env.Environment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) IOException(java.io.IOException)

Example 14 with Environment

use of org.springframework.core.env.Environment in project nikita-noark5-core by HiOA-ABI.

the class N5CoreApp method main.

/**
     * Main method, used to run the application.
     *
     * @param args the command line arguments
     * @throws UnknownHostException if the local host name could not be resolved into an address
     */
public static void main(final String... args) throws UnknownHostException {
    //SpringApplication app = new SpringApplication(N5CoreApp.class);
    //SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
    ConfigurableApplicationContext context = SpringApplication.run(CONFIGS, args);
    context.getBean(AfterApplicationStartup.class).afterApplicationStarts();
    Environment env = context.getEnvironment();
    String[] activeProfiles = env.getActiveProfiles();
    String profilesAsString = Arrays.toString(activeProfiles);
    logger.info("\n----------------------------------------------------------\n\t" + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t\thttp://localhost:{}\n\t" + "External: \t\thttp://{}:{}\n\t" + "contextPath: \thttp://{}:{}{} \n\t" + "Application is running with following profile(s): {} \n\t" + "\n----------------------------------------------------------", env.getProperty("spring.application.name"), env.getProperty("server.port"), InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"), InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"), env.getProperty("server.contextPath"), profilesAsString);
    String configServerStatus = env.getProperty("configserver.status");
    logger.info("\n----------------------------------------------------------\n\t" + "Config Server: \t{}\n----------------------------------------------------------", configServerStatus == null ? "Not found or not setup for this application" : configServerStatus);
    if (profilesAsString != null && profilesAsString.contains("de")) {
        logger.info("\n----------------------------------------------------------\n\t" + "Dev/demo mode: In-memory database ({}) in use. See http://localhost:8082 . Use following JDBC-string: jdbc:h2:mem:n5DemoDb/jdbc:h2:mem:n5DevDb" + "\n----------------------------------------------------------", env.getProperty("spring.jpa.database"));
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Environment(org.springframework.core.env.Environment)

Aggregations

Environment (org.springframework.core.env.Environment)14 Test (org.junit.Test)5 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)3 MutablePropertySources (org.springframework.core.env.MutablePropertySources)3 MockEnvironment (org.springframework.mock.env.MockEnvironment)3 Map (java.util.Map)2 Bean (org.springframework.context.annotation.Bean)2 PropertiesPropertySource (org.springframework.core.env.PropertiesPropertySource)2 NOPAction (ch.qos.logback.core.joran.action.NOPAction)1 ElementSelector (ch.qos.logback.core.joran.spi.ElementSelector)1 AnnotationBean (com.alibaba.dubbo.config.spring.AnnotationBean)1 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1