Search in sources :

Example 36 with RuntimeBeanReference

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

the class ScheduledTasksBeanDefinitionParser method beanReference.

private RuntimeBeanReference beanReference(Element taskElement, ParserContext parserContext, BeanDefinitionBuilder builder) {
    // Extract the source of the current task
    builder.getRawBeanDefinition().setSource(parserContext.extractSource(taskElement));
    String generatedName = parserContext.getReaderContext().generateBeanName(builder.getRawBeanDefinition());
    parserContext.registerBeanComponent(new BeanComponentDefinition(builder.getBeanDefinition(), generatedName));
    return new RuntimeBeanReference(generatedName);
}
Also used : BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 37 with RuntimeBeanReference

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

the class AnnotationProcessorPerformanceTests method testPrototypeCreationWithOverriddenResourcePropertiesIsFastEnough.

@Test
public void testPrototypeCreationWithOverriddenResourcePropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();
    RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 38 with RuntimeBeanReference

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

the class AnnotationProcessorPerformanceTests method testPrototypeCreationWithOverriddenAutowiredPropertiesIsFastEnough.

@Test
public void testPrototypeCreationWithOverriddenAutowiredPropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();
    RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 39 with RuntimeBeanReference

use of org.springframework.beans.factory.config.RuntimeBeanReference in project camel by apache.

the class CamelNamespaceHandler method registerEndpoint.

private void registerEndpoint(Element childElement, ParserContext parserContext, String contextId) {
    String id = childElement.getAttribute("id");
    // must have an id to be registered
    if (ObjectHelper.isNotEmpty(id)) {
        BeanDefinition definition = endpointParser.parse(childElement, parserContext);
        definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
        // Need to add this dependency of CamelContext for Spring 3.0
        try {
            Method method = definition.getClass().getMethod("setDependsOn", String[].class);
            method.invoke(definition, (Object) new String[] { contextId });
        } catch (Exception e) {
        // Do nothing here
        }
        parserContext.registerBeanComponent(new BeanComponentDefinition(definition, id));
    }
}
Also used : BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) Method(java.lang.reflect.Method) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) JAXBException(javax.xml.bind.JAXBException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Example 40 with RuntimeBeanReference

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

the class ResourcesBeanDefinitionParser method parseResourceCache.

private void parseResourceCache(ManagedList<? super Object> resourceResolvers, ManagedList<? super Object> resourceTransformers, Element element, Object source) {
    String resourceCache = element.getAttribute("resource-cache");
    if ("true".equals(resourceCache)) {
        ConstructorArgumentValues cavs = new ConstructorArgumentValues();
        RootBeanDefinition cachingResolverDef = new RootBeanDefinition(CachingResourceResolver.class);
        cachingResolverDef.setSource(source);
        cachingResolverDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        cachingResolverDef.setConstructorArgumentValues(cavs);
        RootBeanDefinition cachingTransformerDef = new RootBeanDefinition(CachingResourceTransformer.class);
        cachingTransformerDef.setSource(source);
        cachingTransformerDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        cachingTransformerDef.setConstructorArgumentValues(cavs);
        String cacheManagerName = element.getAttribute("cache-manager");
        String cacheName = element.getAttribute("cache-name");
        if (StringUtils.hasText(cacheManagerName) && StringUtils.hasText(cacheName)) {
            RuntimeBeanReference cacheManagerRef = new RuntimeBeanReference(cacheManagerName);
            cavs.addIndexedArgumentValue(0, cacheManagerRef);
            cavs.addIndexedArgumentValue(1, cacheName);
        } else {
            ConstructorArgumentValues cacheCavs = new ConstructorArgumentValues();
            cacheCavs.addIndexedArgumentValue(0, RESOURCE_CHAIN_CACHE);
            RootBeanDefinition cacheDef = new RootBeanDefinition(ConcurrentMapCache.class);
            cacheDef.setSource(source);
            cacheDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
            cacheDef.setConstructorArgumentValues(cacheCavs);
            cavs.addIndexedArgumentValue(0, cacheDef);
        }
        resourceResolvers.add(cachingResolverDef);
        resourceTransformers.add(cachingTransformerDef);
    }
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

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