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

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

Introduction

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

Prototype

@Override
    public void refresh() throws BeansException, IllegalStateException 

Source Link

Usage

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

/**
 * From the arguments, run the validation.
 *
 * @param args command line arguments/* w  ww  . j  ava 2 s.c  o 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:net.ggtools.maven.DDLGeneratorMojo.java

AnnotationConfigApplicationContext createApplicationContext() {
    final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    final ConfigurableEnvironment environment = applicationContext.getEnvironment();
    final MutablePropertySources propertySources = environment.getPropertySources();
    final MapPropertySource propertySource = createPropertySource();
    propertySources.addFirst(propertySource);
    applicationContext.register(SpringConfiguration.class);
    applicationContext.refresh();
    return applicationContext;
}

From source file:com.avanza.astrix.integration.tests.GsRemotingTest.java

@Test
public void routedServiceInvocationThrowServiceUnavailableWhenProxyIsContextIsClosed() throws Exception {
    AnnotationConfigApplicationContext pingServer = autoClosables.add(new AnnotationConfigApplicationContext());
    pingServer.register(PingAppConfig.class);
    pingServer.getEnvironment().getPropertySources()
            .addFirst(new MapPropertySource("props", new HashMap<String, Object>() {
                {/*from ww w.j  a  v  a 2  s . c om*/
                    put("serviceRegistryUri", serviceRegistry.getServiceUri());
                }
            }));
    pingServer.refresh();

    AstrixContext context = autoClosables.add(new TestAstrixConfigurer().registerApiProvider(PingApi.class)
            .set(AstrixSettings.SERVICE_REGISTRY_URI, serviceRegistry.getServiceUri())
            .set(AstrixSettings.BEAN_BIND_ATTEMPT_INTERVAL, 200).configure());
    Ping ping = context.waitForBean(Ping.class, 10000);

    assertEquals("foo", ping.ping("foo"));

    context.destroy();

    assertThrows(() -> ping.ping("foo"), ServiceUnavailableException.class);
}

From source file:com.avanza.astrix.integration.tests.GsRemotingTest.java

@Test
public void broadcastedServiceInvocationThrowServiceUnavailableWhenProxyIsContextIsClosed() throws Exception {
    AnnotationConfigApplicationContext pingServer = autoClosables.add(new AnnotationConfigApplicationContext());
    pingServer.register(PingAppConfig.class);
    pingServer.getEnvironment().getPropertySources()
            .addFirst(new MapPropertySource("props", new HashMap<String, Object>() {
                {//from  w  w  w.  j  ava2  s  .  c  o m
                    put("serviceRegistryUri", serviceRegistry.getServiceUri());
                }
            }));
    pingServer.refresh();

    AstrixContext context = autoClosables.add(new TestAstrixConfigurer().registerApiProvider(PingApi.class)
            .set(AstrixSettings.SERVICE_REGISTRY_URI, serviceRegistry.getServiceUri())
            .set(AstrixSettings.BEAN_BIND_ATTEMPT_INTERVAL, 200).configure());
    Ping ping = context.waitForBean(Ping.class, 10000);

    assertEquals("foo", ping.broadcastPing("foo").get(0));

    context.destroy();

    assertThrows(() -> ping.broadcastPing("foo"), ServiceUnavailableException.class);
}

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

@Test
public void withImplicitName() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithImplicitName.class);
    ctx.refresh();
    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 w w  w  . j a  v a2 s.c  o m
}

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

@Test
public void withExplicitName() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithExplicitName.class);
    ctx.refresh();
    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;//  w  ww .  ja v  a 2  s .c o m
    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:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withUnresolvablePlaceholderAndDefault() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithUnresolvablePlaceholderAndDefault.class);
    ctx.refresh();
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
    ctx.close();//  w ww .  jav  a2  s .c o m

}

From source file:com.azaptree.services.spring.application.config.SpringApplicationServiceConfig.java

public AnnotationConfigApplicationContext createAnnotationConfigApplicationContext() {
    final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    if (ArrayUtils.isNotEmpty(springProfiles)) {
        ctx.getEnvironment().setActiveProfiles(springProfiles);
    }/*from  w w w. ja va2  s  .  c  o  m*/

    if (ArrayUtils.isNotEmpty(configurationClasses)) {
        for (final Class<?> c : configurationClasses) {
            ctx.register(c);
        }
    }

    ctx.refresh();
    ctx.registerShutdownHook();
    return ctx;
}

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

@Test
public void withEmptyResourceLocations() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithEmptyResourceLocations.class);
    try {//ww w .  java 2 s. co m
        ctx.refresh();
    } catch (BeanCreationException ex) {
        assertTrue(ex.getCause() instanceof IllegalArgumentException);
    }
    ctx.close();

}

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

@Test
public void withUnresolvablePlaceholder() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithUnresolvablePlaceholder.class);
    try {/*from  w ww .  j a v a  2  s  .c om*/
        ctx.refresh();
    } catch (BeanCreationException ex) {
        assertTrue(ex.getCause() instanceof IllegalArgumentException);
    }
    ctx.close();

}