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

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

Introduction

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

Prototype

String[] getDefaultProfiles();

Source Link

Document

Return the set of profiles to be active by default when no active profiles have been set explicitly.

Usage

From source file:com.setronica.ucs.server.MessageServer.java

private static ClassPathXmlApplicationContext initApplication(String profile,
        DefaultListableBeanFactory parentBeanFactory) {
    ClassPathXmlApplicationContext applicationContext;

    if (parentBeanFactory != null) {
        //wrap BeanFactory inside ApplicationContext
        GenericApplicationContext parentContext = new GenericApplicationContext(parentBeanFactory);
        parentContext.refresh();//from  w  w w .  ja  v a 2 s.  c o  m
        applicationContext = new ClassPathXmlApplicationContext(parentContext);
    } else {
        applicationContext = new ClassPathXmlApplicationContext();
    }
    ConfigurableEnvironment environment = applicationContext.getEnvironment();
    environment.setDefaultProfiles(profile);
    applicationContext.setConfigLocation("spring-application.xml");

    // log active profile

    String[] profiles = environment.getActiveProfiles();
    if (profiles.length == 0) {
        profiles = environment.getDefaultProfiles();
    }
    for (String activeProfile : profiles) {
        if (environment.acceptsProfiles(activeProfile)) {
            logger.info("Profile " + activeProfile + " is active");
        }
    }

    applicationContext.refresh();
    return applicationContext;
}

From source file:org.springframework.cloud.bootstrap.config.ConfigServiceBootstrapConfiguration.java

@Bean
public ConfigServicePropertySourceLocator configServicePropertySource(ConfigurableEnvironment environment) {
    ConfigServicePropertySourceLocator locator = new ConfigServicePropertySourceLocator();
    String[] profiles = environment.getActiveProfiles();
    if (profiles.length == 0) {
        profiles = environment.getDefaultProfiles();
    }/*w w  w .  j a  v  a  2  s.co  m*/
    locator.setEnv(StringUtils.arrayToCommaDelimitedString(profiles));
    return locator;
}

From source file:org.springframework.core.env.AbstractEnvironment.java

@Override
public void merge(ConfigurableEnvironment parent) {
    for (PropertySource<?> ps : parent.getPropertySources()) {
        if (!this.propertySources.contains(ps.getName())) {
            this.propertySources.addLast(ps);
        }/*  www .ja  va  2  s  .  c  om*/
    }
    String[] parentActiveProfiles = parent.getActiveProfiles();
    if (!ObjectUtils.isEmpty(parentActiveProfiles)) {
        synchronized (this.activeProfiles) {
            for (String profile : parentActiveProfiles) {
                this.activeProfiles.add(profile);
            }
        }
    }
    String[] parentDefaultProfiles = parent.getDefaultProfiles();
    if (!ObjectUtils.isEmpty(parentDefaultProfiles)) {
        synchronized (this.defaultProfiles) {
            this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME);
            for (String profile : parentDefaultProfiles) {
                this.defaultProfiles.add(profile);
            }
        }
    }
}