Search in sources :

Example 76 with RuntimeBeanReference

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

the class DefaultListableBeanFactoryTests method testExtensiveCircularReference.

@Test
public void testExtensiveCircularReference() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    for (int i = 0; i < 1000; i++) {
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0))));
        RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
        bd.setPropertyValues(pvs);
        lbf.registerBeanDefinition("bean" + i, bd);
    }
    lbf.preInstantiateSingletons();
    for (int i = 0; i < 1000; i++) {
        TestBean bean = (TestBean) lbf.getBean("bean" + i);
        TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0));
        assertTrue(bean.getSpouse() == otherBean);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyValue(org.springframework.beans.PropertyValue) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Test(org.junit.Test)

Example 77 with RuntimeBeanReference

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

the class DefaultListableBeanFactoryTests method testPrototypeCreationWithResolvedPropertiesIsFastEnough.

@Test
public void testPrototypeCreationWithResolvedPropertiesIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
    lbf.registerBeanDefinition("test", rbd);
    lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) lbf.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    // System.out.println(sw.getTotalTimeMillis());
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 78 with RuntimeBeanReference

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

the class DefinitionMetadataEqualsHashCodeTests method runtimeBeanReference.

@Test
public void runtimeBeanReference() {
    RuntimeBeanReference master = new RuntimeBeanReference("name");
    RuntimeBeanReference equal = new RuntimeBeanReference("name");
    RuntimeBeanReference notEqual = new RuntimeBeanReference("someOtherName");
    RuntimeBeanReference subclass = new RuntimeBeanReference("name") {
    };
    assertEqualsAndHashCodeContracts(master, equal, notEqual, subclass);
}
Also used : RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Test(org.junit.Test)

Example 79 with RuntimeBeanReference

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

the class AbstractListenerContainerParser method parseListener.

private void parseListener(Element containerEle, Element listenerEle, ParserContext parserContext, PropertyValues commonContainerProperties, PropertyValues specificContainerProperties) {
    RootBeanDefinition listenerDef = new RootBeanDefinition();
    listenerDef.setSource(parserContext.extractSource(listenerEle));
    listenerDef.setBeanClassName("org.springframework.jms.listener.adapter.MessageListenerAdapter");
    String ref = listenerEle.getAttribute(REF_ATTRIBUTE);
    if (!StringUtils.hasText(ref)) {
        parserContext.getReaderContext().error("Listener 'ref' attribute contains empty value.", listenerEle);
    } else {
        listenerDef.getPropertyValues().add("delegate", new RuntimeBeanReference(ref));
    }
    String method = null;
    if (listenerEle.hasAttribute(METHOD_ATTRIBUTE)) {
        method = listenerEle.getAttribute(METHOD_ATTRIBUTE);
        if (!StringUtils.hasText(method)) {
            parserContext.getReaderContext().error("Listener 'method' attribute contains empty value.", listenerEle);
        }
    }
    listenerDef.getPropertyValues().add("defaultListenerMethod", method);
    PropertyValue messageConverterPv = commonContainerProperties.getPropertyValue("messageConverter");
    if (messageConverterPv != null) {
        listenerDef.getPropertyValues().addPropertyValue(messageConverterPv);
    }
    BeanDefinition containerDef = createContainer(containerEle, listenerEle, parserContext, commonContainerProperties, specificContainerProperties);
    containerDef.getPropertyValues().add("messageListener", listenerDef);
    if (listenerEle.hasAttribute(RESPONSE_DESTINATION_ATTRIBUTE)) {
        String responseDestination = listenerEle.getAttribute(RESPONSE_DESTINATION_ATTRIBUTE);
        Boolean pubSubDomain = (Boolean) commonContainerProperties.getPropertyValue("replyPubSubDomain").getValue();
        listenerDef.getPropertyValues().add(pubSubDomain ? "defaultResponseTopicName" : "defaultResponseQueueName", responseDestination);
        if (containerDef.getPropertyValues().contains("destinationResolver")) {
            listenerDef.getPropertyValues().add("destinationResolver", containerDef.getPropertyValues().getPropertyValue("destinationResolver").getValue());
        }
    }
    String containerBeanName = listenerEle.getAttribute(ID_ATTRIBUTE);
    // If no bean id is given auto generate one using the ReaderContext's BeanNameGenerator
    if (!StringUtils.hasText(containerBeanName)) {
        containerBeanName = parserContext.getReaderContext().generateBeanName(containerDef);
    }
    // Register the listener and fire event
    parserContext.registerBeanComponent(new BeanComponentDefinition(containerDef, containerBeanName));
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) PropertyValue(org.springframework.beans.PropertyValue) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 80 with RuntimeBeanReference

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

the class AbstractListenerContainerParser method parseCommonContainerProperties.

protected MutablePropertyValues parseCommonContainerProperties(Element containerEle, ParserContext parserContext) {
    MutablePropertyValues properties = new MutablePropertyValues();
    String destinationType = containerEle.getAttribute(DESTINATION_TYPE_ATTRIBUTE);
    boolean pubSubDomain = false;
    boolean subscriptionDurable = false;
    boolean subscriptionShared = false;
    if (DESTINATION_TYPE_SHARED_DURABLE_TOPIC.equals(destinationType)) {
        pubSubDomain = true;
        subscriptionDurable = true;
        subscriptionShared = true;
    } else if (DESTINATION_TYPE_SHARED_TOPIC.equals(destinationType)) {
        pubSubDomain = true;
        subscriptionShared = true;
    } else if (DESTINATION_TYPE_DURABLE_TOPIC.equals(destinationType)) {
        pubSubDomain = true;
        subscriptionDurable = true;
    } else if (DESTINATION_TYPE_TOPIC.equals(destinationType)) {
        pubSubDomain = true;
    } else if ("".equals(destinationType) || DESTINATION_TYPE_QUEUE.equals(destinationType)) {
    // the default: queue
    } else {
        parserContext.getReaderContext().error("Invalid listener container 'destination-type': only " + "\"queue\", \"topic\", \"durableTopic\", \"sharedTopic\", \"sharedDurableTopic\" supported.", containerEle);
    }
    properties.add("pubSubDomain", pubSubDomain);
    properties.add("subscriptionDurable", subscriptionDurable);
    properties.add("subscriptionShared", subscriptionShared);
    boolean replyPubSubDomain = false;
    String replyDestinationType = containerEle.getAttribute(RESPONSE_DESTINATION_TYPE_ATTRIBUTE);
    if (DESTINATION_TYPE_TOPIC.equals(replyDestinationType)) {
        replyPubSubDomain = true;
    } else if (DESTINATION_TYPE_QUEUE.equals(replyDestinationType)) {
        replyPubSubDomain = false;
    } else if (!StringUtils.hasText(replyDestinationType)) {
        // the default: same value as pubSubDomain
        replyPubSubDomain = pubSubDomain;
    } else if (StringUtils.hasText(replyDestinationType)) {
        parserContext.getReaderContext().error("Invalid listener container 'response-destination-type': only " + "\"queue\", \"topic\" supported.", containerEle);
    }
    properties.add("replyPubSubDomain", replyPubSubDomain);
    if (containerEle.hasAttribute(CLIENT_ID_ATTRIBUTE)) {
        String clientId = containerEle.getAttribute(CLIENT_ID_ATTRIBUTE);
        if (!StringUtils.hasText(clientId)) {
            parserContext.getReaderContext().error("Listener 'client-id' attribute contains empty value.", containerEle);
        }
        properties.add("clientId", clientId);
    }
    if (containerEle.hasAttribute(MESSAGE_CONVERTER_ATTRIBUTE)) {
        String messageConverter = containerEle.getAttribute(MESSAGE_CONVERTER_ATTRIBUTE);
        if (!StringUtils.hasText(messageConverter)) {
            parserContext.getReaderContext().error("listener container 'message-converter' attribute contains empty value.", containerEle);
        } else {
            properties.add("messageConverter", new RuntimeBeanReference(messageConverter));
        }
    }
    return properties;
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

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