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 Object getBean(String name) throws BeansException 

Source Link

Usage

From source file:scriptella.driver.spring.EtlExecutorBean.java

/**
 * This method obtains a global ThreadLocal class independent of the classloader (JVM-scope singleton).
 * The easiest solution is to use System.getProperties().get/put, but this solution violate
 * Properties contract and have other drawbacks.
 * <p>Current solution relies on the idea behind
 * {@link org.springframework.beans.factory.access.SingletonBeanFactoryLocator}. See also bug #4648
 *
 * @return Global ThreadLocal (JVM-scope singleton).
 *///from  w w w .jav  a  2 s  .  c  om
@SuppressWarnings("unchecked")
private static ThreadLocal<BeanFactory> getGlobalThreadLocal() {
    BeanFactoryLocator locator = SingletonBeanFactoryLocator.getInstance(BEAN_FACTORY_XML_PATH);
    BeanFactoryReference ref = locator.useBeanFactory(FACTORY_BEAN_NAME);
    StaticApplicationContext ctx = (StaticApplicationContext) ref.getFactory();
    if (!ctx.containsBean(THREAD_LOCAL_BEAN_NAME)) {
        ctx.registerSingleton(THREAD_LOCAL_BEAN_NAME, ThreadLocal.class);
    }
    return (ThreadLocal) ctx.getBean(THREAD_LOCAL_BEAN_NAME);
}

From source file:com.github.pjungermann.config.types.DefaultConfigFactorySelectorTest.java

@Test
public void beanCreation_withSpringContextAndNoFactory_noErrorAndEmptyListAsDefault() {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(DefaultConfigFactorySelector.class.getName(), DefaultConfigFactorySelector.class);
    ConfigFactorySelector selector = context.getBean(ConfigFactorySelector.class);

    assertValidBean(selector);/*from ww w.  ja  v  a  2  s. c  o  m*/
}

From source file:com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurerTest.java

@Test
public void testProcessPropertiesConvertAndUpdate() throws Exception {
    configurer.setPropertyLoaders(new MockPropertyLoader[] { new MockPropertyLoader(properties) });

    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(ConvertableTestBean.class);
    beanFactory.registerBeanDefinition(TEST_BEAN_NAME, beanDefinition);

    properties.put(TEST_KEY, "1");

    configurer.loadProperties(properties);
    configurer.processProperties(beanFactory, properties);

    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(TEST_BEAN_NAME, ConvertableTestBean.class);
    configurer.setApplicationContext(context);

    configurer.setBeanFactory(beanFactory);
    configurer.propertyChanged(new PropertyEvent(this, TEST_KEY, "2"));

    assertEquals(2, ((ConvertableTestBean) context.getBean(TEST_BEAN_NAME)).getProperty());
    assertNull(System.getProperties().get(TEST_KEY));
}

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

/**
 * [preHandle] Case of CodeList is one.//from   ww  w.j  a  v a2s  .c o m
 * <p>
 * [Expected Result]
 * <ol>
 * <li>set CodeList to attribute of HttpServletRequest.</li>
 * </ol>
 * </p>
 * @throws Exception
 */
@Test
public void testPreHandle_one() throws Exception {

    // do setup.
    StaticApplicationContext mockApplicationContext = new StaticApplicationContext();
    mockApplicationContext.registerSingleton("simpleMapCodeList", SimpleMapCodeList.class);
    SimpleMapCodeList simpleMapCodeList = mockApplicationContext.getBean(SimpleMapCodeList.class);
    simpleMapCodeList.setMap(Collections.singletonMap("key", "value"));

    testTarget.setApplicationContext(mockApplicationContext);
    testTarget.afterPropertiesSet();

    // do test.
    boolean actualReturnValue = testTarget.preHandle(mockRequest, mockResponse, null);

    // do assert.
    Enumeration<String> actualAttributeNames = mockRequest.getAttributeNames();
    assertThat(actualAttributeNames.hasMoreElements(), is(true));
    actualAttributeNames.nextElement();
    assertThat(actualAttributeNames.hasMoreElements(), is(false));
    assertThat(mockRequest.getAttribute("simpleMapCodeList").toString(),
            is(simpleMapCodeList.asMap().toString()));
    assertThat(actualReturnValue, is(true));

}

From source file:com.urbanmania.spring.beans.factory.config.annotations.PropertyAnnotationAndPlaceholderConfigurerTest.java

@Test
public void testProcessPropertiesAndUpdate() throws Exception {
    configurer.setPropertyLoaders(new MockPropertyLoader[] { new MockPropertyLoader(properties) });

    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(UpdateableTestBean.class);
    beanFactory.registerBeanDefinition(TEST_BEAN_NAME, beanDefinition);

    properties.put(TEST_KEY, TEST_VALUE);

    configurer.loadProperties(properties);
    configurer.processProperties(beanFactory, properties);

    StaticApplicationContext context = new StaticApplicationContext();
    context.registerSingleton(TEST_BEAN_NAME, UpdateableTestBean.class);
    configurer.setApplicationContext(context);

    configurer.setBeanFactory(beanFactory);
    configurer.propertyChanged(new PropertyEvent(this, TEST_KEY, TEST_CHANGED_VALUE));

    assertEquals(TEST_CHANGED_VALUE, ((UpdateableTestBean) context.getBean(TEST_BEAN_NAME)).getProperty());
    assertNull(System.getProperties().get(TEST_KEY));
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_methodAnnotatedWithMessageMappingAnnotation_methodInvokedForIncomingMessage()
        throws Exception {
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandler", IncomingMessageHandler.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();/*  ww w . java2  s  .  c om*/

    MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class);
    messageHandler.handleMessage(MessageBuilder.withPayload("testContent")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "receive").build());

    IncomingMessageHandler messageListener = applicationContext.getBean(IncomingMessageHandler.class);
    assertEquals("testContent", messageListener.getLastReceivedMessage());
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveAndReplyMessage_methodAnnotatedWithMessageMappingAnnotation_methodInvokedForIncomingMessageAndReplySentBackToSendToDestination()
        throws Exception {
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandler", IncomingMessageHandler.class);
    applicationContext.registerBeanDefinition("queueMessageHandler", getQueueMessageHandlerBeanDefinition());
    applicationContext.refresh();//from  ww  w  .ja  va2 s  .c  o m

    MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class);
    messageHandler.handleMessage(MessageBuilder.withPayload("testContent")
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "receiveAndReply")
            .build());

    IncomingMessageHandler messageListener = applicationContext.getBean(IncomingMessageHandler.class);
    assertEquals("testContent", messageListener.getLastReceivedMessage());
    verify(this.messageTemplate).convertAndSend(eq("sendTo"), eq("TESTCONTENT"));
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withNotificationMessageAndSubject_shouldResolveThem() throws Exception {
    // Arrange/*from w  w  w.  jav  a 2 s .  c  o  m*/
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("notificationMessageReceiver", NotificationMessageReceiver.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    NotificationMessageReceiver notificationMessageReceiver = applicationContext
            .getBean(NotificationMessageReceiver.class);

    ObjectNode jsonObject = JsonNodeFactory.instance.objectNode();
    jsonObject.put("Type", "Notification");
    jsonObject.put("Subject", "Hi!");
    jsonObject.put("Message", "Hello World!");
    String payload = jsonObject.toString();

    // Act
    queueMessageHandler.handleMessage(MessageBuilder.withPayload(payload)
            .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")
            .build());

    // Assert
    assertEquals("Hi!", notificationMessageReceiver.getSubject());
    assertEquals("Hello World!", notificationMessageReceiver.getMessage());
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_methodWithCustomObjectAsParameter_parameterIsConverted() throws Exception {
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("incomingMessageHandler",
            IncomingMessageHandlerWithCustomParameter.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();/*from w w  w. j  a  v  a2s.com*/

    MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class);
    DummyKeyValueHolder messagePayload = new DummyKeyValueHolder("myKey", "A value");
    MappingJackson2MessageConverter jsonMapper = new MappingJackson2MessageConverter();
    Message<?> message = jsonMapper.toMessage(messagePayload,
            new MessageHeaders(Collections.<String, Object>singletonMap(
                    QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")));
    messageHandler.handleMessage(message);

    IncomingMessageHandlerWithCustomParameter messageListener = applicationContext
            .getBean(IncomingMessageHandlerWithCustomParameter.class);
    assertNotNull(messageListener.getLastReceivedMessage());
    assertEquals("myKey", messageListener.getLastReceivedMessage().getKey());
    assertEquals("A value", messageListener.getLastReceivedMessage().getValue());
}

From source file:org.springframework.cloud.aws.messaging.listener.QueueMessageHandlerTest.java

@Test
public void receiveMessage_withHeaderAnnotationAsArgument_shouldReceiveRequestedHeader() throws Exception {
    // Arrange/*from   w ww.j  a  va2s.  c o m*/
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("messageHandlerWithHeaderAnnotation",
            MessageReceiverWithHeaderAnnotation.class);
    applicationContext.registerSingleton("queueMessageHandler", QueueMessageHandler.class);
    applicationContext.refresh();

    QueueMessageHandler queueMessageHandler = applicationContext.getBean(QueueMessageHandler.class);
    MessageReceiverWithHeaderAnnotation messageReceiver = applicationContext
            .getBean(MessageReceiverWithHeaderAnnotation.class);

    // Act
    queueMessageHandler.handleMessage(
            MessageBuilder.withPayload("Hello from a sender").setHeader("SenderId", "elsUnitTest")
                    .setHeader(QueueMessageHandler.Headers.LOGICAL_RESOURCE_ID_MESSAGE_HEADER_KEY, "testQueue")
                    .build());

    // Assert
    assertEquals("Hello from a sender", messageReceiver.getPayload());
    assertEquals("elsUnitTest", messageReceiver.getSenderId());
}