Search in sources :

Example 81 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class JcaListenerContainerParser method parseSpecificContainerProperties.

@Override
protected MutablePropertyValues parseSpecificContainerProperties(Element containerEle, ParserContext parserContext) {
    MutablePropertyValues properties = new MutablePropertyValues();
    if (containerEle.hasAttribute(RESOURCE_ADAPTER_ATTRIBUTE)) {
        String resourceAdapterBeanName = containerEle.getAttribute(RESOURCE_ADAPTER_ATTRIBUTE);
        if (!StringUtils.hasText(resourceAdapterBeanName)) {
            parserContext.getReaderContext().error("Listener container 'resource-adapter' attribute contains empty value.", containerEle);
        } else {
            properties.add("resourceAdapter", new RuntimeBeanReference(resourceAdapterBeanName));
        }
    }
    String activationSpecFactoryBeanName = containerEle.getAttribute(ACTIVATION_SPEC_FACTORY_ATTRIBUTE);
    String destinationResolverBeanName = containerEle.getAttribute(DESTINATION_RESOLVER_ATTRIBUTE);
    if (StringUtils.hasText(activationSpecFactoryBeanName)) {
        if (StringUtils.hasText(destinationResolverBeanName)) {
            parserContext.getReaderContext().error("Specify either 'activation-spec-factory' or " + "'destination-resolver', not both. If you define a dedicated JmsActivationSpecFactory bean, " + "specify the custom DestinationResolver there (if possible).", containerEle);
        }
        properties.add("activationSpecFactory", new RuntimeBeanReference(activationSpecFactoryBeanName));
    }
    if (StringUtils.hasText(destinationResolverBeanName)) {
        properties.add("destinationResolver", new RuntimeBeanReference(destinationResolverBeanName));
    }
    String transactionManagerBeanName = containerEle.getAttribute(TRANSACTION_MANAGER_ATTRIBUTE);
    if (StringUtils.hasText(transactionManagerBeanName)) {
        properties.add("transactionManager", new RuntimeBeanReference(transactionManagerBeanName));
    }
    String phase = containerEle.getAttribute(PHASE_ATTRIBUTE);
    if (StringUtils.hasText(phase)) {
        properties.add("phase", phase);
    }
    return properties;
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 82 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class JmsListenerContainerParser method parseSpecificContainerProperties.

@Override
protected MutablePropertyValues parseSpecificContainerProperties(Element containerEle, ParserContext parserContext) {
    MutablePropertyValues properties = new MutablePropertyValues();
    boolean isSimpleContainer = containerEle.getAttribute(CONTAINER_TYPE_ATTRIBUTE).startsWith("simple");
    String connectionFactoryBeanName = "connectionFactory";
    if (containerEle.hasAttribute(CONNECTION_FACTORY_ATTRIBUTE)) {
        connectionFactoryBeanName = containerEle.getAttribute(CONNECTION_FACTORY_ATTRIBUTE);
        if (!StringUtils.hasText(connectionFactoryBeanName)) {
            parserContext.getReaderContext().error("Listener container 'connection-factory' attribute contains empty value.", containerEle);
        }
    }
    if (StringUtils.hasText(connectionFactoryBeanName)) {
        properties.add("connectionFactory", new RuntimeBeanReference(connectionFactoryBeanName));
    }
    String taskExecutorBeanName = containerEle.getAttribute(TASK_EXECUTOR_ATTRIBUTE);
    if (StringUtils.hasText(taskExecutorBeanName)) {
        properties.add("taskExecutor", new RuntimeBeanReference(taskExecutorBeanName));
    }
    String errorHandlerBeanName = containerEle.getAttribute(ERROR_HANDLER_ATTRIBUTE);
    if (StringUtils.hasText(errorHandlerBeanName)) {
        properties.add("errorHandler", new RuntimeBeanReference(errorHandlerBeanName));
    }
    String destinationResolverBeanName = containerEle.getAttribute(DESTINATION_RESOLVER_ATTRIBUTE);
    if (StringUtils.hasText(destinationResolverBeanName)) {
        properties.add("destinationResolver", new RuntimeBeanReference(destinationResolverBeanName));
    }
    String cache = containerEle.getAttribute(CACHE_ATTRIBUTE);
    if (StringUtils.hasText(cache)) {
        if (isSimpleContainer) {
            if (!("auto".equals(cache) || "consumer".equals(cache))) {
                parserContext.getReaderContext().warning("'cache' attribute not actively supported for listener container of type \"simple\". " + "Effective runtime behavior will be equivalent to \"consumer\" / \"auto\".", containerEle);
            }
        } else {
            properties.add("cacheLevelName", "CACHE_" + cache.toUpperCase());
        }
    }
    Integer acknowledgeMode = parseAcknowledgeMode(containerEle, parserContext);
    if (acknowledgeMode != null) {
        if (acknowledgeMode == Session.SESSION_TRANSACTED) {
            properties.add("sessionTransacted", Boolean.TRUE);
        } else {
            properties.add("sessionAcknowledgeMode", acknowledgeMode);
        }
    }
    String transactionManagerBeanName = containerEle.getAttribute(TRANSACTION_MANAGER_ATTRIBUTE);
    if (StringUtils.hasText(transactionManagerBeanName)) {
        if (isSimpleContainer) {
            parserContext.getReaderContext().error("'transaction-manager' attribute not supported for listener container of type \"simple\".", containerEle);
        } else {
            properties.add("transactionManager", new RuntimeBeanReference(transactionManagerBeanName));
        }
    }
    String concurrency = containerEle.getAttribute(CONCURRENCY_ATTRIBUTE);
    if (StringUtils.hasText(concurrency)) {
        properties.add("concurrency", concurrency);
    }
    String prefetch = containerEle.getAttribute(PREFETCH_ATTRIBUTE);
    if (StringUtils.hasText(prefetch)) {
        if (!isSimpleContainer) {
            properties.add("maxMessagesPerTask", prefetch);
        }
    }
    String phase = containerEle.getAttribute(PHASE_ATTRIBUTE);
    if (StringUtils.hasText(phase)) {
        properties.add("phase", phase);
    }
    String receiveTimeout = containerEle.getAttribute(RECEIVE_TIMEOUT_ATTRIBUTE);
    if (StringUtils.hasText(receiveTimeout)) {
        if (!isSimpleContainer) {
            properties.add("receiveTimeout", receiveTimeout);
        }
    }
    String backOffBeanName = containerEle.getAttribute(BACK_OFF_ATTRIBUTE);
    if (StringUtils.hasText(backOffBeanName)) {
        if (!isSimpleContainer) {
            properties.add("backOff", new RuntimeBeanReference(backOffBeanName));
        }
    } else {
        // No need to consider this if back-off is set
        String recoveryInterval = containerEle.getAttribute(RECOVERY_INTERVAL_ATTRIBUTE);
        if (StringUtils.hasText(recoveryInterval)) {
            if (!isSimpleContainer) {
                properties.add("recoveryInterval", recoveryInterval);
            }
        }
    }
    return properties;
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 83 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class ApplicationContextEventTests method listenersInApplicationContext.

@Test
public void listenersInApplicationContext() {
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBeanDefinition("listener1", new RootBeanDefinition(MyOrderedListener1.class));
    RootBeanDefinition listener2 = new RootBeanDefinition(MyOrderedListener2.class);
    listener2.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("listener1"));
    listener2.setLazyInit(true);
    context.registerBeanDefinition("listener2", listener2);
    context.refresh();
    assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2"));
    MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
    MyOtherEvent event1 = new MyOtherEvent(context);
    context.publishEvent(event1);
    assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2"));
    MyEvent event2 = new MyEvent(context);
    context.publishEvent(event2);
    assertTrue(context.getDefaultListableBeanFactory().containsSingleton("listener2"));
    MyEvent event3 = new MyEvent(context);
    context.publishEvent(event3);
    MyOtherEvent event4 = new MyOtherEvent(context);
    context.publishEvent(event4);
    assertTrue(listener1.seenEvents.contains(event1));
    assertTrue(listener1.seenEvents.contains(event2));
    assertTrue(listener1.seenEvents.contains(event3));
    assertTrue(listener1.seenEvents.contains(event4));
    listener1.seenEvents.clear();
    context.publishEvent(event1);
    context.publishEvent(event2);
    context.publishEvent(event3);
    context.publishEvent(event4);
    assertTrue(listener1.seenEvents.contains(event1));
    assertTrue(listener1.seenEvents.contains(event2));
    assertTrue(listener1.seenEvents.contains(event3));
    assertTrue(listener1.seenEvents.contains(event4));
    AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
    assertEquals(2, multicaster.retrieverCache.size());
    context.close();
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Test(org.junit.Test)

Example 84 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class PropertyResourceConfigurerIntegrationTests method testPropertyPlaceholderConfigurerWithSystemPropertyInLocation.

@Test
public void testPropertyPlaceholderConfigurerWithSystemPropertyInLocation() {
    StaticApplicationContext ac = new StaticApplicationContext();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("spouse", new RuntimeBeanReference("${ref}"));
    ac.registerSingleton("tb", TestBean.class, pvs);
    pvs = new MutablePropertyValues();
    pvs.add("location", "${user.dir}/test");
    ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
    try {
        ac.refresh();
        fail("Should have thrown BeanInitializationException");
    } catch (BeanInitializationException ex) {
        // expected
        assertTrue(ex.getCause() instanceof FileNotFoundException);
        // slight hack for Linux/Unix systems
        String userDir = StringUtils.cleanPath(System.getProperty("user.dir"));
        if (userDir.startsWith("/")) {
            userDir = userDir.substring(1);
        }
        assertTrue(ex.getMessage().indexOf(userDir) != -1);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) FileNotFoundException(java.io.FileNotFoundException) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Test(org.junit.Test)

Example 85 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project spring-framework by spring-projects.

the class JeeNamespaceHandlerTests method testWithEnvironment.

@Test
public void testWithEnvironment() throws Exception {
    BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("withEnvironment");
    assertPropertyValue(beanDefinition, "jndiEnvironment", "foo=bar");
    assertPropertyValue(beanDefinition, "defaultObject", new RuntimeBeanReference("myBean"));
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Test(org.junit.Test)

Aggregations

RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)156 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)86 Element (org.w3c.dom.Element)47 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)39 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)33 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)32 ManagedList (org.springframework.beans.factory.support.ManagedList)27 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)24 Test (org.junit.Test)21 ManagedMap (org.springframework.beans.factory.support.ManagedMap)20 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)15 GroovyObject (groovy.lang.GroovyObject)12 TestBean (org.springframework.tests.sample.beans.TestBean)12 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 Map (java.util.Map)10 Node (org.w3c.dom.Node)10 HashMap (java.util.HashMap)9 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)9 ITestBean (org.springframework.tests.sample.beans.ITestBean)8