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

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

Introduction

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

Prototype

@Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
            throws BeanDefinitionStoreException 

Source Link

Usage

From source file:io.nuun.plugin.spring.UsingSpringAsDIPlugin.java

public Object nativeUnitModule() {
    ClassPathXmlApplicationContext parentCtx = new ClassPathXmlApplicationContext("context.xml");

    StaticApplicationContext dynCtx = new StaticApplicationContext();
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(Service3Internal.class);
    beanDef.setScope("prototype");
    dynCtx.registerBeanDefinition("service3", beanDef);

    dynCtx.setParent(parentCtx);//from ww w  .jav  a2  s.c o m
    dynCtx.refresh();

    return dynCtx;
}

From source file:org.nuunframework.spring.UsingSpringAsDIPlugin.java

@Override
public Object dependencyInjectionDef() {
    ClassPathXmlApplicationContext parentCtx = new ClassPathXmlApplicationContext("context.xml");

    StaticApplicationContext dynCtx = new StaticApplicationContext();
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(Service3Internal.class);
    beanDef.setScope("prototype");
    dynCtx.registerBeanDefinition("service3", beanDef);

    dynCtx.setParent(parentCtx);/*  ww w  . j a  v a 2  s  . com*/
    dynCtx.refresh();

    return dynCtx;
}

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();/*  w  ww  .j  a  v  a2  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:net.frontlinesms.FrontlineSMS.java

/** Initialise {@link #applicationContext}. */
public void initApplicationContext() throws DataAccessResourceFailureException {
    // Load the data mode from the app.properties file
    AppProperties appProperties = AppProperties.getInstance();

    LOG.info("Load Spring/Hibernate application context to initialise DAOs");

    // Create a base ApplicationContext defining the hibernate config file we need to import
    StaticApplicationContext baseApplicationContext = new StaticApplicationContext();
    baseApplicationContext.registerBeanDefinition("hibernateConfigLocations",
            createHibernateConfigLocationsBeanDefinition());

    // Get the spring config locations
    String databaseExternalConfigPath = ResourceUtils.getConfigDirectoryPath()
            + ResourceUtils.PROPERTIES_DIRECTORY_NAME + File.separatorChar
            + appProperties.getDatabaseConfigPath();
    String[] configLocations = getSpringConfigLocations(databaseExternalConfigPath);
    baseApplicationContext.refresh();// w w w .  j  av  a2 s  . com

    FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext(configLocations,
            false, baseApplicationContext);
    this.applicationContext = applicationContext;

    // Add post-processor to handle substituted database properties
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    String databasePropertiesPath = ResourceUtils.getConfigDirectoryPath()
            + ResourceUtils.PROPERTIES_DIRECTORY_NAME + File.separatorChar
            + appProperties.getDatabaseConfigPath() + ".properties";
    propertyPlaceholderConfigurer.setLocation(new FileSystemResource(new File(databasePropertiesPath)));
    propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
    applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);
    applicationContext.refresh();

    LOG.info("Context loaded successfully.");

    this.pluginManager = new PluginManager(this, applicationContext);

    LOG.info("Getting DAOs from application context...");
    groupDao = (GroupDao) applicationContext.getBean("groupDao");
    groupMembershipDao = (GroupMembershipDao) applicationContext.getBean("groupMembershipDao");
    contactDao = (ContactDao) applicationContext.getBean("contactDao");
    keywordDao = (KeywordDao) applicationContext.getBean("keywordDao");
    keywordActionDao = (KeywordActionDao) applicationContext.getBean("keywordActionDao");
    messageDao = (MessageDao) applicationContext.getBean("messageDao");
    emailDao = (EmailDao) applicationContext.getBean("emailDao");
    emailAccountDao = (EmailAccountDao) applicationContext.getBean("emailAccountDao");
    smsInternetServiceSettingsDao = (SmsInternetServiceSettingsDao) applicationContext
            .getBean("smsInternetServiceSettingsDao");
    smsModemSettingsDao = (SmsModemSettingsDao) applicationContext.getBean("smsModemSettingsDao");
    eventBus = (EventBus) applicationContext.getBean("eventBus");
}

From source file:ome.client.utests.Preferences3Test.java

@Test
public void test_makeOurOwnDefault() throws Exception {
    // Others://w  ww . ja va  2 s  .  c o  m
    // new ManagedMap();
    // BeanWrapper bw = new BeanWrapperImpl( defaultMap );

    Map defaultMap = new HashMap();
    defaultMap.put("omero.user", "foo");
    ConstructorArgumentValues cav = new ConstructorArgumentValues();
    cav.addGenericArgumentValue(defaultMap);
    BeanDefinition def = new RootBeanDefinition(HashMap.class, cav, null);
    StaticApplicationContext ac = new StaticApplicationContext();
    ac.registerBeanDefinition("map", def);
    ac.refresh();

    ConstructorArgumentValues testCav = new ConstructorArgumentValues();
    testCav.addGenericArgumentValue(new RuntimeBeanReference("map"));
    BeanDefinition testDef = new RootBeanDefinition(HashMap.class, testCav, null);
    StaticApplicationContext defaultTest = new StaticApplicationContext(ac);
    defaultTest.registerBeanDefinition("test", testDef);
    defaultTest.refresh();
    assertTrue("foo".equals(((Map) defaultTest.getBean("test")).get("omero.user")));

}

From source file:ome.client.utests.Preferences3Test.java

@Test
public void test_makeOurOwnRuntime() throws Exception {

    // use properties
    // if no Properties given, then is static (global)

    Map runtimeMap = new HashMap();
    runtimeMap.put("omero.user", "bar");
    ConstructorArgumentValues cav2 = new ConstructorArgumentValues();
    cav2.addGenericArgumentValue(runtimeMap);
    BeanDefinition def2 = new RootBeanDefinition(HashMap.class, cav2, null);
    StaticApplicationContext ac2 = new StaticApplicationContext();
    ac2.registerBeanDefinition("map", def2);
    ac2.refresh();//w w w  .  j  a v  a 2  s .  c  o m

    ConstructorArgumentValues testCav2 = new ConstructorArgumentValues();
    testCav2.addGenericArgumentValue(new RuntimeBeanReference("map"));
    BeanDefinition testDef2 = new RootBeanDefinition(HashMap.class, testCav2, null);
    StaticApplicationContext defaultTest2 = new StaticApplicationContext(ac2);
    defaultTest2.registerBeanDefinition("test", testDef2);
    defaultTest2.refresh();
    assertTrue("bar".equals(((Map) defaultTest2.getBean("test")).get("omero.user")));

}

From source file:ome.client.utests.Preferences3Test.java

@Test(groups = "ticket:1058")
public void testOmeroUserIsProperlySetWithSpring2_5_5Manual() {

    Server s = new Server("localhost", 1099);
    Login l = new Login("me", "password");
    Properties p = s.asProperties();
    p.putAll(l.asProperties());//ww  w  .  j  av  a  2 s. c  o m

    // This is copied from OmeroContext. This is the parent context which
    // should contain the properties;
    Properties copy = new Properties(p);
    ConstructorArgumentValues ctorArg1 = new ConstructorArgumentValues();
    ctorArg1.addGenericArgumentValue(copy);
    BeanDefinition definition1 = new RootBeanDefinition(Properties.class, ctorArg1, null);
    StaticApplicationContext staticContext = new StaticApplicationContext();
    staticContext.registerBeanDefinition("properties", definition1);
    staticContext.refresh();

    // This is the child context and contains a definition of a
    // PlaceHolderConfigurer
    // as well as a user of
    StaticApplicationContext childContext = new StaticApplicationContext();

    MutablePropertyValues mpv2 = new MutablePropertyValues();
    mpv2.addPropertyValue("properties", new RuntimeBeanReference("properties"));
    mpv2.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_FALLBACK");
    mpv2.addPropertyValue("localOverride", "true");
    BeanDefinition definitionConfigurer = new RootBeanDefinition(PreferencesPlaceholderConfigurer.class, null,
            mpv2);
    childContext.registerBeanDefinition("propertiesPlaceholderConfigurer", definitionConfigurer);

    ConstructorArgumentValues cav2 = new ConstructorArgumentValues();
    cav2.addGenericArgumentValue("${omero.user}");
    BeanDefinition definitionTest = new RootBeanDefinition(String.class, cav2, null);
    childContext.registerBeanDefinition("test", definitionTest);

    childContext.setParent(staticContext);
    childContext.refresh();

    String test = (String) childContext.getBean("test");
    assertEquals(test, "me");

}

From source file:org.springframework.integration.handler.ExpressionEvaluatingMessageProcessorTests.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test/*  w  w w.  j av  a  2s  .  c  o  m*/
public void testProcessMessageWithBeanAsMethodArgument() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    BeanDefinition beanDefinition = new RootBeanDefinition(String.class);
    beanDefinition.getConstructorArgumentValues().addGenericArgumentValue("bar");
    context.registerBeanDefinition("testString", beanDefinition);
    context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME,
            new RootBeanDefinition(IntegrationEvaluationContextFactoryBean.class));
    Expression expression = expressionParser.parseExpression("payload.concat(@testString)");
    ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
    processor.setBeanFactory(context);
    processor.afterPropertiesSet();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    assertEquals("foobar", processor.processMessage(message));
}

From source file:org.springframework.integration.handler.ExpressionEvaluatingMessageProcessorTests.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test/*  w ww.j av a 2s. c  o  m*/
public void testProcessMessageWithMethodCallOnBean() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    BeanDefinition beanDefinition = new RootBeanDefinition(String.class);
    beanDefinition.getConstructorArgumentValues().addGenericArgumentValue("bar");
    context.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME,
            new RootBeanDefinition(IntegrationEvaluationContextFactoryBean.class));
    context.registerBeanDefinition("testString", beanDefinition);
    Expression expression = expressionParser.parseExpression("@testString.concat(payload)");
    ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression);
    processor.setBeanFactory(context);
    processor.afterPropertiesSet();
    GenericMessage<String> message = new GenericMessage<String>("foo");
    assertEquals("barfoo", processor.processMessage(message));
}

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  w  w  w .ja  va 2  s. co  m
    context.refresh();
    assertTrue(channel.isRunning());
    context.stop();
    assertFalse(channel.isRunning());
    context.close();
}