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

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

Introduction

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

Prototype

public void setConfigLocation(String location) 

Source Link

Document

Set the config locations for this application context in init-param style, i.e.

Usage

From source file:il.ac.tau.yoavram.pes.SpringRunner.java

public static void run(String[] args) throws IOException {
    System.out.println("Starting " + SpringRunner.class.getSimpleName());
    SimulationConfigurer configurer = new SimulationConfigurer(args);
    if (configurer.getSpringXmlConfig() == null) {
        System.err.println("Spring XML config file not defined");
        System.err.println();/*from w ww.jav a 2  s.c  o m*/
        System.exit(1);
    }
    if (configurer.getProperties() == null) {
        System.err.println("Properties not defined");
        System.err.println();
        System.exit(1);
    }

    // get the properties
    Properties properties = configurer.getProperties();
    String jobName = properties.getProperty(SimulationConfigurer.JOB_NAME_KEY);

    // create context
    AbstractXmlApplicationContext context = new ClassPathXmlApplicationContext();

    // add properties to context
    logger.info("Adding properties to context: " + properties.toString());
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    propertyPlaceholderConfigurer.setProperties(properties);
    context.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);

    // set config location
    String configLocation = configurer.getSpringXmlConfig().toString();
    logger.info("Loading context from file " + configLocation);
    context.setConfigLocation(configLocation);

    // make sure destroy methods will be called and refresh the context
    context.registerShutdownHook();
    context.refresh();

    // persist properties
    try {
        PropertiesPersister persister = context.getBean(PropertiesPersister.class);
        if (persister != null) {
            persister.persist(properties);
        }
    } catch (NoSuchBeanDefinitionException e) {
        // nothing to do
    }

    // get the simulation bean and run it
    Simulation simulation = context.getBean("simulation", Simulation.class);

    logger.debug("Starting simulation " + jobName);
    simulation.start();
}

From source file:de.hoegertn.demo.cxfsimple.SpringStarter.java

public final void doStart() throws Exception {
    try {//from ww w  .  j  a v a2  s. 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();

        final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setProperties(System.getProperties());
        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);
    }
}

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

@Override
public final void doStart() throws Exception {
    super.doStart();
    try {// ww  w.  ja va 2  s.  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);
    }
}