Example usage for org.springframework.context ConfigurableApplicationContext setEnvironment

List of usage examples for org.springframework.context ConfigurableApplicationContext setEnvironment

Introduction

In this page you can find the example usage for org.springframework.context ConfigurableApplicationContext setEnvironment.

Prototype

void setEnvironment(ConfigurableEnvironment environment);

Source Link

Document

Set the Environment for this application context.

Usage

From source file:com.github.exper0.efilecopier.ftp.DynamicFtpChannelResolver.java

/**
 * Use Spring 3.1. environment support to set properties for the
 * customer-specific application context.
 *
 * @param ctx/* w w w .  j a v  a 2 s.c o m*/
 * @param customer
 */
private void setEnvironmentForCustomer(ConfigurableApplicationContext ctx, String customer) {

    StandardEnvironment env = new StandardEnvironment();
    Properties props = new Properties();
    // populate properties for customer
    props.setProperty("remote.directory", "/");
    props.setProperty("session.factory", "session.factory");
    PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
    env.getPropertySources().addLast(pps);
    ctx.setEnvironment(env);
}

From source file:com.rllc.batch.webcast.DynamicFtpChannelResolver.java

/**
 * Use Spring 3.1. environment support to set properties for the
 * file-specific application context./*from   w  w w . j av  a2 s . c  om*/
 *
 * @param ctx
 * @param customer
 */
private void setEnvironmentForFile(ConfigurableApplicationContext ctx, String fileName) {
    StandardEnvironment environment = new StandardEnvironment();
    Properties props = new Properties();

    // populate properties for file
    props.setProperty("host", env.getProperty("batch.sftp.hostname"));
    props.setProperty("user", env.getProperty("batch.sftp.username"));
    props.setProperty("password", env.getProperty("batch.sftp.password"));
    props.setProperty("port", env.getProperty("batch.sftp.port"));
    props.setProperty("remote.directory", getRemoteDirectoryForFile(fileName));
    PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
    environment.getPropertySources().addLast(pps);
    ctx.setEnvironment(environment);
}

From source file:org.springframework.boot.SpringApplication.java

private ConfigurableApplicationContext doRun(SpringApplicationRunListeners listeners, String... args) {
    ConfigurableApplicationContext context;
    // Create and configure the environment
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    configureEnvironment(environment, args);
    listeners.environmentPrepared(environment);
    if (isWebEnvironment(environment) && !this.webEnvironment) {
        environment = convertToStandardEnvironment(environment);
    }// www  .ja v  a  2 s.  co m

    if (this.bannerMode != Banner.Mode.OFF) {
        printBanner(environment);
    }

    // Create, load, refresh and run the ApplicationContext
    context = createApplicationContext();
    context.setEnvironment(environment);
    postProcessApplicationContext(context);
    applyInitializers(context);
    listeners.contextPrepared(context);
    if (this.logStartupInfo) {
        logStartupInfo(context.getParent() == null);
        logStartupProfileInfo(context);
    }

    // Add boot specific singleton beans
    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments);

    // Load the sources
    Set<Object> sources = getSources();
    Assert.notEmpty(sources, "Sources must not be empty");
    load(context, sources.toArray(new Object[sources.size()]));
    listeners.contextLoaded(context);

    // Refresh the context
    refresh(context);
    if (this.registerShutdownHook) {
        try {
            context.registerShutdownHook();
        } catch (AccessControlException ex) {
            // Not allowed in some environments.
        }
    }
    afterRefresh(context, applicationArguments);
    listeners.finished(context, null);
    return context;
}

From source file:org.springframework.xd.dirt.stream.FileSourceModuleTests.java

@Test
public void testSplitterUsesIterator() throws Exception {
    System.out.println(System.getProperty("user.dir"));
    ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(
            new String[] { "../modules/common/file-source-common-context.xml",
                    "classpath:org/springframework/xd/dirt/stream/ppc-context.xml" },
            false);//from  ww  w  .j  av  a2 s . com
    StandardEnvironment env = new StandardEnvironment();
    Properties props = new Properties();
    props.setProperty("fixedDelay", "5");
    props.setProperty("timeUnit", "SECONDS");
    props.setProperty("initialDelay", "0");
    props.setProperty("withMarkers", "false");
    PropertiesPropertySource pps = new PropertiesPropertySource("props", props);
    env.getPropertySources().addLast(pps);
    env.setActiveProfiles("use-contents-with-split");
    ctx.setEnvironment(env);
    ctx.refresh();
    FileSplitter splitter = ctx.getBean(FileSplitter.class);
    File foo = File.createTempFile("foo", ".txt");
    final AtomicReference<Method> splitMessage = new AtomicReference<>();
    ReflectionUtils.doWithMethods(FileSplitter.class, new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            method.setAccessible(true);
            splitMessage.set(method);
        }
    }, new MethodFilter() {

        @Override
        public boolean matches(Method method) {
            return method.getName().equals("splitMessage");
        }
    });
    Object result = splitMessage.get().invoke(splitter, new GenericMessage<File>(foo));
    assertThat(result, instanceOf(Iterator.class));
    ctx.close();
    foo.delete();
}