Example usage for org.springframework.context.support AbstractXmlApplicationContext getEnvironment

List of usage examples for org.springframework.context.support AbstractXmlApplicationContext getEnvironment

Introduction

In this page you can find the example usage for org.springframework.context.support AbstractXmlApplicationContext getEnvironment.

Prototype

@Override
public ConfigurableEnvironment getEnvironment() 

Source Link

Document

Return the Environment for this application context in configurable form, allowing for further customization.

Usage

From source file:de.taimos.daemon.spring.SpringDaemonAdapter.java

@Override
public final void doStart() throws Exception {
    super.doStart();
    try {//from   w w  w  .j  a  va  2s. c om
        this.doBeforeSpringStart();
    } catch (Exception e) {
        throw new RuntimeException("Before spring failed", e);
    }

    Lock writeLock = this.rwLock.writeLock();
    AbstractXmlApplicationContext ctx = null;
    try {
        writeLock.lock();
        if (this.context.get() != null) {
            throw new RuntimeException("Already started");
        }
        ctx = this.createSpringContext();
        String[] profiles = System.getProperty(Configuration.PROFILES, Configuration.PROFILES_PRODUCTION)
                .split(",");
        ctx.getEnvironment().setActiveProfiles(profiles);

        final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setProperties(DaemonStarter.getDaemonProperties());
        ctx.addBeanFactoryPostProcessor(configurer);

        ctx.setConfigLocation(this.getSpringResource());
        ctx.refresh();
    } catch (Exception e) {
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e1) {
                this.logger.warn("Failed to close context", e1);
            }
            ctx = null;
        }
        throw new RuntimeException("Spring context failed", e);
    } finally {
        if (ctx != null) {
            this.context.set(ctx);
        }
        writeLock.unlock();
    }

    try {
        this.doAfterSpringStart();
    } catch (Exception e) {
        throw new RuntimeException("After spring failed", e);
    }
}