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

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

Introduction

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

Prototype

void setDefaultProfiles(String... profiles);

Source Link

Document

Specify the set of profiles to be made active by default if no other profiles are explicitly made active through #setActiveProfiles .

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();// w  w w.j  ava 2s .  com
        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.ojb.web.portal.WebPortalApplicationContextInitializer.java

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

    Properties props = new Properties();
    String activeProfiles = "";

    try {/*from www .  j  a v a 2  s.  co m*/
        props.load(applicationContext.getClassLoader()
                .getResourceAsStream("config/ojb_web_external_resource.cfg"));
        activeProfiles = props.getProperty("spring.activeProfiles");
    } catch (IOException e) {
        LOG.info(
                "Unable to load spring profiles from external resource, will load default from ojb_web_portal.cfg");
    }

    if (StringUtils.isBlank(activeProfiles)) {
        try {
            props.clear();
            props.load(applicationContext.getClassLoader().getResourceAsStream("config/ojb-web-portal.cfg"));
            activeProfiles = props.getProperty("spring.activeProfiles");
        } catch (IOException e) {
            LOG.info("Unable to load spring profiles from ojb_web_portal.cfg");
        }
    }

    if (StringUtils.isBlank(activeProfiles)) {
        LOG.info("No spring profiles set, will run in 'standalone' mode");
        environment.setDefaultProfiles("standalone");
    } else {
        LOG.info("Setting default profiles to:" + activeProfiles);
        environment.setDefaultProfiles(activeProfiles.split(","));
    }
}