Example usage for org.springframework.context.annotation AnnotationConfigApplicationContext close

List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext close

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigApplicationContext close.

Prototype

@Override
public void close() 

Source Link

Document

Close this application context, destroying all beans in its bean factory.

Usage

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

/**
 * Tests the LIFO behavior of @EncryptablePropertySource annotaitons. The
 * last one registered should 'win'.//from  w ww. jav  a2s  . c o m
 */
@Test
public void orderingIsLifo() {
    {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(ConfigWithImplicitName.class, P2Config.class);
        ctx.refresh();
        // p2 should 'win' as it was registered last
        assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p2TestBean"));
        ctx.close();
    }

    {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(P2Config.class, ConfigWithImplicitName.class);
        ctx.refresh();
        // p1 should 'win' as it was registered last
        assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
        ctx.close();
    }
}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withResolvablePlaceholder() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithResolvablePlaceholder.class);
    System.setProperty("path.to.properties", "org/jasypt/spring31/annotation");
    ctx.refresh();/*  w  w w .  ja va 2s . co  m*/
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
    System.clearProperty("path.to.properties");
    ctx.close();

}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withResolvablePlaceholderAndFactoryBean() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithResolvablePlaceholderAndFactoryBean.class);
    System.setProperty("path.to.properties", "org/jasypt/spring31/annotation");
    ctx.refresh();/*from   ww  w . j  a  va  2s.c  om*/
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
    System.clearProperty("path.to.properties");
    ctx.close();

}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withMultipleResourceLocations() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
            ConfigWithMultipleResourceLocations.class);
    assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
    assertThat(ctx.getEnvironment().containsProperty("from.p3"), is(true));
    // p2 should 'win' as it was registered last
    assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p3TestBean"));
    ctx.close();
}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withImplicitName() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithImplicitName.class);
    ctx.refresh();//from   w w w  .  j a v  a2s .  c  om
    assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources()
            .contains("class path resource [org/jasypt/spring31/annotation/basic-encrypted-p1.properties]"));
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
    ctx.close();
}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withNameAndMultipleResourceLocations() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
            ConfigWithNameAndMultipleResourceLocations.class);
    assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true));
    assertThat(ctx.getEnvironment().containsProperty("from.p3"), is(true));
    // p2 should 'win' as it was registered last
    assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p3TestBean"));
    ctx.close();
}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void orderingWithAndWithoutNameAndMultipleResourceLocations() {
    // SPR-10820: p2 should 'win' as it was registered last
    AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(
            ConfigWithNameAndMultipleResourceLocations.class);
    AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(
            ConfigWithMultipleResourceLocations.class);
    assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p3TestBean"));
    assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p3TestBean"));
    ctxWithName.close();
    ctxWithoutName.close();/*  w w  w  . j a v  a  2s  .co m*/
}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withExplicitName() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithExplicitName.class);
    ctx.refresh();//  w  ww  .ja v a 2 s  .co m
    assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources().contains("p1"));
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));

    // assert that the property source was added last to the set of sources
    String name;
    MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
    Iterator<org.springframework.core.env.PropertySource<?>> iterator = sources.iterator();
    do {
        name = iterator.next().getName();
    } while (iterator.hasNext());

    assertThat(name, is("p1"));
    ctx.close();
}

From source file:com.cloudera.cli.validator.Main.java

/**
 * From the arguments, run the validation.
 *
 * @param args command line arguments/*from w w  w .j av a  2s  . co  m*/
 * @throws IOException anything goes wrong with streams.
 * @return exit code
 */
public int run(String[] args) throws IOException {
    Writer writer = new OutputStreamWriter(outStream, Constants.CHARSET_UTF_8);
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    try {
        BeanDefinition cmdOptsbeanDefinition = BeanDefinitionBuilder
                .rootBeanDefinition(CommandLineOptions.class).addConstructorArgValue(appName)
                .addConstructorArgValue(args).getBeanDefinition();
        ctx.registerBeanDefinition(CommandLineOptions.BEAN_NAME, cmdOptsbeanDefinition);
        ctx.register(ApplicationConfiguration.class);
        ctx.refresh();
        CommandLineOptions cmdOptions = ctx.getBean(CommandLineOptions.BEAN_NAME, CommandLineOptions.class);
        CommandLineOptions.Mode mode = cmdOptions.getMode();
        if (mode == null) {
            throw new ParseException("No valid command line arguments");
        }
        ValidationRunner runner = ctx.getBean(mode.runnerName, ValidationRunner.class);
        boolean success = runner.run(cmdOptions.getCommandLineOptionActiveTarget(), writer);
        if (success) {
            writer.write("Validation succeeded.\n");
        }
        return success ? 0 : -1;
    } catch (BeanCreationException e) {
        String cause = e.getMessage();
        if (e.getCause() instanceof BeanInstantiationException) {
            BeanInstantiationException bie = (BeanInstantiationException) e.getCause();
            cause = bie.getMessage();
            if (bie.getCause() != null) {
                cause = bie.getCause().getMessage();
            }
        }
        IOUtils.write(cause + "\n", errStream);
        CommandLineOptions.printUsageMessage(appName, errStream);
        return -2;
    } catch (ParseException e) {
        LOG.debug("Exception", e);
        IOUtils.write(e.getMessage() + "\n", errStream);
        CommandLineOptions.printUsageMessage(appName, errStream);
        return -2;
    } finally {
        if (ctx != null) {
            ctx.close();
        }
        writer.close();
    }
}

From source file:com.ryantenney.metrics.spring.EnableMetricsTest.java

@Test
public void metricsConfigTest() throws Throwable {
    // Initialize the context
    AnnotationConfigApplicationContext applicationContext = null;
    try {//www.  j av  a 2 s  .  c o  m
        applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MetricsConfig.class);
        applicationContext.refresh();

        // Assert that the custom registries were used
        assertSame(metricRegistry, applicationContext.getBean(MetricRegistry.class));
        assertSame(healthCheckRegistry, applicationContext.getBean(HealthCheckRegistry.class));

        // Verify that the configureReporters method was invoked
        assertThat(MetricsConfig.isConfigureReportersInvoked, is(true));

        // Assert that the bean has been proxied
        TestBean testBean = applicationContext.getBean(TestBean.class);
        assertNotNull(testBean);
        assertThat(AopUtils.isAopProxy(testBean), is(true));

        // Verify that the Gauge field's value is returned
        Gauge<Integer> fieldGauge = (Gauge<Integer>) forGaugeField(metricRegistry, TestBean.class,
                "intGaugeField");
        assertNotNull(fieldGauge);
        assertThat(fieldGauge.getValue(), is(5));

        // Verify that the Gauge method's value is returned
        Gauge<Integer> methodGauge = (Gauge<Integer>) forGaugeMethod(metricRegistry, TestBean.class,
                "intGaugeMethod");
        assertNotNull(methodGauge);
        assertThat(methodGauge.getValue(), is(6));

        // Verify that the Timer's counter is incremented on method invocation
        Timer timedMethodTimer = forTimedMethod(metricRegistry, TestBean.class, "timedMethod");
        assertNotNull(timedMethodTimer);
        assertThat(timedMethodTimer.getCount(), is(0L));
        testBean.timedMethod();
        assertThat(timedMethodTimer.getCount(), is(1L));

        // Verify that the Meter's counter is incremented on method invocation
        Meter meteredMethodMeter = forMeteredMethod(metricRegistry, TestBean.class, "meteredMethod");
        assertNotNull(meteredMethodMeter);
        assertThat(meteredMethodMeter.getCount(), is(0L));
        testBean.meteredMethod();
        assertThat(meteredMethodMeter.getCount(), is(1L));

        // Verify that the Meter's counter is incremented on method invocation
        Meter exceptionMeteredMethodMeter = forExceptionMeteredMethod(metricRegistry, TestBean.class,
                "exceptionMeteredMethod");
        assertNotNull(exceptionMeteredMethodMeter);
        assertThat(exceptionMeteredMethodMeter.getCount(), is(0L));
        try {
            testBean.exceptionMeteredMethod();
        } catch (Throwable t) {
        }
        assertThat(exceptionMeteredMethodMeter.getCount(), is(1L));
    } finally {
        if (applicationContext != null) {
            applicationContext.close();
        }
    }
}