Example usage for org.springframework.context.support ClassPathXmlApplicationContext refresh

List of usage examples for org.springframework.context.support ClassPathXmlApplicationContext refresh

Introduction

In this page you can find the example usage for org.springframework.context.support ClassPathXmlApplicationContext refresh.

Prototype

@Override
    public void refresh() throws BeansException, IllegalStateException 

Source Link

Usage

From source file:org.springframework.data.neo4j.illegal.aspects.index2.IllegalIndex2Tests.java

/**
 * As the first illegal entity detected will blow up the application context - we need a way
 * to ensure only the illegal entity under test it loaded to assert that we fail
 * for the correct reason and in an appropriate way. This method will create and application
 * context ensuring that only the illegal entity under test (passed in as an argument), is
 * detected by the context.  This is currently done by wrapping each Illegal Entity bootstrap
 * logic in a Spring profile against its same name
 *
 * @param entityUnderTest Class which should be detected by SDN for the purposes of testing
 * @throws Throwable//from   ww  w.ja  v  a  2s.c o m
 */
private void createAppCtxAndPropagateRootExceptionIfThrown(Class entityUnderTest) throws Throwable {
    try {
        ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext();
        appCtx.setConfigLocation(
                "org/springframework/data/neo4j/aspects/support/illegal-index2-tests-context.xml");
        appCtx.getEnvironment().setActiveProfiles(entityUnderTest.getSimpleName());
        appCtx.refresh();
    } catch (BeanCreationException bce) {
        throw ExceptionUtils.getRootCause(bce);
    }
}

From source file:org.springframework.integration.channel.DirectChannelTests.java

@Test //  See INT-2434
public void testChannelCreationWithBeanDefinitionOverrideTrue() throws Exception {
    ClassPathXmlApplicationContext parentContext = new ClassPathXmlApplicationContext("parent-config.xml",
            this.getClass());
    MessageChannel parentChannelA = parentContext.getBean("parentChannelA", MessageChannel.class);
    MessageChannel parentChannelB = parentContext.getBean("parentChannelB", MessageChannel.class);

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
    context.setAllowBeanDefinitionOverriding(false);
    context.setConfigLocations(//from  w  w  w . j  av a 2 s.c om
            new String[] { "classpath:org/springframework/integration/channel/channel-override-config.xml" });
    context.setParent(parentContext);
    Method method = ReflectionUtils.findMethod(ClassPathXmlApplicationContext.class, "obtainFreshBeanFactory");
    method.setAccessible(true);
    method.invoke(context);
    assertFalse(context.containsBean("channelA"));
    assertFalse(context.containsBean("channelB"));
    assertTrue(context.containsBean("channelC"));
    assertTrue(context.containsBean("channelD"));

    context.refresh();

    PublishSubscribeChannel channelEarly = context.getBean("channelEarly", PublishSubscribeChannel.class);

    assertTrue(context.containsBean("channelA"));
    assertTrue(context.containsBean("channelB"));
    assertTrue(context.containsBean("channelC"));
    assertTrue(context.containsBean("channelD"));
    EventDrivenConsumer consumerA = context.getBean("serviceA", EventDrivenConsumer.class);
    assertEquals(context.getBean("channelA"), TestUtils.getPropertyValue(consumerA, "inputChannel"));
    assertEquals(context.getBean("channelB"), TestUtils.getPropertyValue(consumerA, "handler.outputChannel"));

    EventDrivenConsumer consumerB = context.getBean("serviceB", EventDrivenConsumer.class);
    assertEquals(context.getBean("channelB"), TestUtils.getPropertyValue(consumerB, "inputChannel"));
    assertEquals(context.getBean("channelC"), TestUtils.getPropertyValue(consumerB, "handler.outputChannel"));

    EventDrivenConsumer consumerC = context.getBean("serviceC", EventDrivenConsumer.class);
    assertEquals(context.getBean("channelC"), TestUtils.getPropertyValue(consumerC, "inputChannel"));
    assertEquals(context.getBean("channelD"), TestUtils.getPropertyValue(consumerC, "handler.outputChannel"));

    EventDrivenConsumer consumerD = context.getBean("serviceD", EventDrivenConsumer.class);
    assertEquals(parentChannelA, TestUtils.getPropertyValue(consumerD, "inputChannel"));
    assertEquals(parentChannelB, TestUtils.getPropertyValue(consumerD, "handler.outputChannel"));

    EventDrivenConsumer consumerE = context.getBean("serviceE", EventDrivenConsumer.class);
    assertEquals(parentChannelB, TestUtils.getPropertyValue(consumerE, "inputChannel"));

    EventDrivenConsumer consumerF = context.getBean("serviceF", EventDrivenConsumer.class);
    assertEquals(channelEarly, TestUtils.getPropertyValue(consumerF, "inputChannel"));
}

From source file:org.springframework.richclient.application.ApplicationLauncher.java

/**
 * Returns an {@code ApplicationContext}, loaded from the bean definition
 * files at the classpath-relative locations specified by
 * {@code configLocations}.//  ww w. j av a  2  s .  c o  m
 *
 * <p>
 * If a splash screen has been created, the application context will be
 * loaded with a bean post processor that will notify the splash screen's
 * progress monitor as each bean is initialized.
 * </p>
 *
 * @param configLocations The classpath-relative locations of the files from
 * which the application context will be loaded.
 *
 * @return The main application context, never null.
 */
private ApplicationContext loadRootApplicationContext(String[] configLocations, MessageSource messageSource) {
    final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            configLocations, false);

    if (splashScreen instanceof MonitoringSplashScreen) {
        final ProgressMonitor tracker = ((MonitoringSplashScreen) splashScreen).getProgressMonitor();

        applicationContext.addBeanFactoryPostProcessor(
                new ProgressMonitoringBeanFactoryPostProcessor(tracker, messageSource));

    }

    applicationContext.refresh();

    return applicationContext;
}

From source file:org.springframework.xd.dirt.launcher.RedisContainerLauncher.java

/**
 * Create a container instance/*from  w  ww  .  j  a va 2 s .c  om*/
 * @param options
 */
@SuppressWarnings("resource")
public static Container create(ContainerOptions options) {
    ClassPathXmlApplicationContext context = null;
    try {
        context = new ClassPathXmlApplicationContext();
        context.setConfigLocation(LAUNCHER_CONFIG_LOCATION);
        //TODO: Need to sort out how this will be handled consistently among launchers
        if (!options.isJmxDisabled()) {
            context.getEnvironment().addActiveProfile("xd.jmx.enabled");
            OptionUtils.setJmxProperties(options, context.getEnvironment());
        }
        context.refresh();
    } catch (BeanCreationException e) {
        if (e.getCause() instanceof RedisConnectionFailureException) {
            logger.fatal(e.getCause().getMessage());
            System.err.println("Redis does not seem to be running. Did you install and start Redis? "
                    + "Please see the Getting Started section of the guide for instructions.");
            System.exit(1);
        }
    }
    context.registerShutdownHook();
    ContainerLauncher launcher = context.getBean(ContainerLauncher.class);
    Container container = launcher.launch(options);
    return container;
}

From source file:org.springframework.xd.perftest.PerformanceTest.java

public static void main(String[] args) {
    updatePerfTestHome();//from w  ww . j  a  v  a2 s  .  c o m
    PerformanceTest perfTest = new PerformanceTest();
    CmdLineOptions options = perfTest.new CmdLineOptions();
    CmdLineParser parser = new CmdLineParser(options);
    parser.setUsageWidth(90);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        parser.printUsage(System.err);
        System.exit(1);
    }
    transport = options.getTransport();
    consumerType = options.getConsumerType();
    flow = options.getFlow();
    setConnectionFactoryType(options.getConnectionFactoryType());
    if (options.isShowHelp()) {
        parser.printUsage(System.err);
        System.exit(0);
    }
    logger.info("*******Starting Performance test********");
    logger.info("Transport: " + transport);
    if (flow.equals(Flow.inbound)) {
        logger.info("Consumer:  " + consumerType);
    }
    logger.info("ConnectionFactory: " + options.getConnectionFactoryType());
    logger.info("****************************************");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
    context.getEnvironment().setActiveProfiles(consumerType.name());
    context.setConfigLocation("META-INF/perftest/" + transport + "-perftest-" + options.getFlow() + ".xml");
    context.refresh();
    context.registerShutdownHook();

}