Example usage for org.springframework.context.support StaticApplicationContext getBean

List of usage examples for org.springframework.context.support StaticApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.context.support StaticApplicationContext getBean.

Prototype

@Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException 

Source Link

Usage

From source file:org.springframework.ws.transport.http.HttpComponentsMessageSenderIntegrationTest.java

@Test
public void testContextClose() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    int port = FreePortScanner.getFreePort();
    Server jettyServer = new Server(port);
    Context jettyContext = new Context(jettyServer, "/");
    jettyContext.addServlet(new ServletHolder(new EchoServlet()), "/");
    jettyServer.start();/*from  w  w w  . ja  v  a  2 s.c  o  m*/
    WebServiceConnection connection = null;
    try {

        StaticApplicationContext appContext = new StaticApplicationContext();
        appContext.registerSingleton("messageSender", HttpComponentsMessageSender.class);
        appContext.refresh();

        HttpComponentsMessageSender messageSender = appContext.getBean("messageSender",
                HttpComponentsMessageSender.class);
        connection = messageSender.createConnection(new URI("http://localhost:" + port));

        connection.send(new SaajSoapMessage(messageFactory.createMessage()));
        connection.receive(new SaajSoapMessageFactory(messageFactory));

        appContext.close();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException ex) {
                // ignore
            }
        }
        if (jettyServer.isRunning()) {
            jettyServer.stop();
        }
    }

}

From source file:org.terasoluna.gfw.web.codelist.CodeListInterceptorTest.java

/**
 * [afterPropertiesSet] Case of define only one bean of codelist type.
 * <p>/*from  w w  w.ja va 2  s .  c  om*/
 * [Expected Result]
 * <ol>
 * <li>target is one.(don't occur error.)</li>
 * </ol>
 * </p>
 * @throws Exception
 */
@Test
public void testAfterPropertiesSet_not_define_one_CodeList() throws Exception {

    // do setup.
    StaticApplicationContext mockApplicationContext = new StaticApplicationContext();
    mockApplicationContext.registerSingleton("simpleMapCodeList", SimpleMapCodeList.class);
    testTarget.setApplicationContext(mockApplicationContext);

    // do test.
    testTarget.afterPropertiesSet();

    // do assert.
    List<CodeList> expectedCodeLists = new ArrayList<CodeList>();
    expectedCodeLists.add(mockApplicationContext.getBean("simpleMapCodeList", CodeList.class));

    assertThat(testTarget.getCodeLists().toString(), is(expectedCodeLists.toString()));

}

From source file:org.springframework.integration.jms.SubscribableJmsChannelTests.java

@Test
public void contextManagesLifecycle() {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(JmsChannelFactoryBean.class);
    builder.addConstructorArgValue(true);
    builder.addPropertyValue("connectionFactory", this.connectionFactory);
    builder.addPropertyValue("destinationName", "dynamicQueue");
    builder.addPropertyValue("pubSubDomain", false);
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBeanDefinition("channel", builder.getBeanDefinition());
    SubscribableJmsChannel channel = context.getBean("channel", SubscribableJmsChannel.class);
    assertFalse(channel.isRunning());//from ww w  .  j a  v a 2  s .c  o  m
    context.refresh();
    assertTrue(channel.isRunning());
    context.stop();
    assertFalse(channel.isRunning());
    context.close();
}

From source file:org.springframework.ws.transport.http.CommonsHttpMessageSenderIntegrationTest.java

@Test
public void testContextClose() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    int port = FreePortScanner.getFreePort();
    Server jettyServer = new Server(port);
    Context jettyContext = new Context(jettyServer, "/");
    jettyContext.addServlet(new ServletHolder(new EchoServlet()), "/");
    jettyServer.start();/*from  ww  w  .ja  v  a  2 s. c  o m*/
    WebServiceConnection connection = null;
    try {

        StaticApplicationContext appContext = new StaticApplicationContext();
        appContext.registerSingleton("messageSender", CommonsHttpMessageSender.class);
        appContext.refresh();

        CommonsHttpMessageSender messageSender = appContext.getBean("messageSender",
                CommonsHttpMessageSender.class);
        connection = messageSender.createConnection(new URI("http://localhost:" + port));

        appContext.close();

        connection.send(new SaajSoapMessage(messageFactory.createMessage()));
        connection.receive(new SaajSoapMessageFactory(messageFactory));
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException ex) {
                // ignore
            }
        }
        if (jettyServer.isRunning()) {
            jettyServer.stop();
        }
    }

}