Search in sources :

Example 11 with GenericBeanDefinition

use of org.springframework.beans.factory.support.GenericBeanDefinition in project spring-framework by spring-projects.

the class SimpleTransactionScopeTests method getFromScope.

@Test
@SuppressWarnings("resource")
public void getFromScope() throws Exception {
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
    GenericBeanDefinition bd1 = new GenericBeanDefinition();
    bd1.setBeanClass(TestBean.class);
    bd1.setScope("tx");
    bd1.setPrimary(true);
    context.registerBeanDefinition("txScopedObject1", bd1);
    GenericBeanDefinition bd2 = new GenericBeanDefinition();
    bd2.setBeanClass(DerivedTestBean.class);
    bd2.setScope("tx");
    context.registerBeanDefinition("txScopedObject2", bd2);
    context.refresh();
    try {
        context.getBean(TestBean.class);
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    try {
        context.getBean(DerivedTestBean.class);
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    TestBean bean1 = null;
    DerivedTestBean bean2 = null;
    DerivedTestBean bean2a = null;
    DerivedTestBean bean2b = null;
    TransactionSynchronizationManager.initSynchronization();
    try {
        bean1 = context.getBean(TestBean.class);
        assertSame(bean1, context.getBean(TestBean.class));
        bean2 = context.getBean(DerivedTestBean.class);
        assertSame(bean2, context.getBean(DerivedTestBean.class));
        context.getBeanFactory().destroyScopedBean("txScopedObject2");
        assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
        assertTrue(bean2.wasDestroyed());
        bean2a = context.getBean(DerivedTestBean.class);
        assertSame(bean2a, context.getBean(DerivedTestBean.class));
        assertNotSame(bean2, bean2a);
        context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
        assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
        assertFalse(bean2a.wasDestroyed());
        bean2b = context.getBean(DerivedTestBean.class);
        assertSame(bean2b, context.getBean(DerivedTestBean.class));
        assertNotSame(bean2, bean2b);
        assertNotSame(bean2a, bean2b);
    } finally {
        TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
        TransactionSynchronizationManager.clearSynchronization();
    }
    assertFalse(bean2a.wasDestroyed());
    assertTrue(bean2b.wasDestroyed());
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    try {
        context.getBean(TestBean.class);
        fail("Should have thrown IllegalStateException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    try {
        context.getBean(DerivedTestBean.class);
        fail("Should have thrown IllegalStateException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 12 with GenericBeanDefinition

use of org.springframework.beans.factory.support.GenericBeanDefinition in project spring-boot by spring-projects.

the class AutoConfigurationPackages method register.

/**
	 * Programmatically registers the auto-configuration package names. Subsequent
	 * invocations will add the given package names to those that have already been
	 * registered. You can use this method to manually define the base packages that will
	 * be used for a given {@link BeanDefinitionRegistry}. Generally it's recommended that
	 * you don't call this method directly, but instead rely on the default convention
	 * where the package name is set from your {@code @EnableAutoConfiguration}
	 * configuration class or classes.
	 * @param registry the bean definition registry
	 * @param packageNames the package names to set
	 */
public static void register(BeanDefinitionRegistry registry, String... packageNames) {
    if (registry.containsBeanDefinition(BEAN)) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(BEAN);
        ConstructorArgumentValues constructorArguments = beanDefinition.getConstructorArgumentValues();
        constructorArguments.addIndexedArgumentValue(0, addBasePackages(constructorArguments, packageNames));
    } else {
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(BasePackages.class);
        beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, packageNames);
        beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        registry.registerBeanDefinition(BEAN, beanDefinition);
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 13 with GenericBeanDefinition

use of org.springframework.beans.factory.support.GenericBeanDefinition in project spring-framework by spring-projects.

the class WebApplicationContextScopeTests method initApplicationContext.

private WebApplicationContext initApplicationContext(String scope) {
    MockServletContext sc = new MockServletContext();
    GenericWebApplicationContext ac = new GenericWebApplicationContext(sc);
    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(DerivedTestBean.class);
    bd.setScope(scope);
    ac.registerBeanDefinition(NAME, bd);
    ac.refresh();
    return ac;
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext)

Example 14 with GenericBeanDefinition

use of org.springframework.beans.factory.support.GenericBeanDefinition in project spring-framework by spring-projects.

the class AnnotationDrivenBeanDefinitionParser method createObjectMapperFactoryDefinition.

private GenericBeanDefinition createObjectMapperFactoryDefinition(Object source) {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(Jackson2ObjectMapperFactoryBean.class);
    beanDefinition.setSource(source);
    beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    return beanDefinition;
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition)

Example 15 with GenericBeanDefinition

use of org.springframework.beans.factory.support.GenericBeanDefinition in project spring-framework by spring-projects.

the class AnnotationDrivenBeanDefinitionParser method getMessageConverters.

private ManagedList<?> getMessageConverters(Element element, Object source, ParserContext parserContext) {
    Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters");
    ManagedList<? super Object> messageConverters = new ManagedList<>();
    if (convertersElement != null) {
        messageConverters.setSource(source);
        for (Element beanElement : DomUtils.getChildElementsByTagName(convertersElement, "bean", "ref")) {
            Object object = parserContext.getDelegate().parsePropertySubElement(beanElement, null);
            messageConverters.add(object);
        }
    }
    if (convertersElement == null || Boolean.valueOf(convertersElement.getAttribute("register-defaults"))) {
        messageConverters.setSource(source);
        messageConverters.add(createConverterDefinition(ByteArrayHttpMessageConverter.class, source));
        RootBeanDefinition stringConverterDef = createConverterDefinition(StringHttpMessageConverter.class, source);
        stringConverterDef.getPropertyValues().add("writeAcceptCharset", false);
        messageConverters.add(stringConverterDef);
        messageConverters.add(createConverterDefinition(ResourceHttpMessageConverter.class, source));
        messageConverters.add(createConverterDefinition(SourceHttpMessageConverter.class, source));
        messageConverters.add(createConverterDefinition(AllEncompassingFormHttpMessageConverter.class, source));
        if (romePresent) {
            messageConverters.add(createConverterDefinition(AtomFeedHttpMessageConverter.class, source));
            messageConverters.add(createConverterDefinition(RssChannelHttpMessageConverter.class, source));
        }
        if (jackson2XmlPresent) {
            RootBeanDefinition jacksonConverterDef = createConverterDefinition(MappingJackson2XmlHttpMessageConverter.class, source);
            GenericBeanDefinition jacksonFactoryDef = createObjectMapperFactoryDefinition(source);
            jacksonFactoryDef.getPropertyValues().add("createXmlMapper", true);
            jacksonConverterDef.getConstructorArgumentValues().addIndexedArgumentValue(0, jacksonFactoryDef);
            messageConverters.add(jacksonConverterDef);
        } else if (jaxb2Present) {
            messageConverters.add(createConverterDefinition(Jaxb2RootElementHttpMessageConverter.class, source));
        }
        if (jackson2Present) {
            RootBeanDefinition jacksonConverterDef = createConverterDefinition(MappingJackson2HttpMessageConverter.class, source);
            GenericBeanDefinition jacksonFactoryDef = createObjectMapperFactoryDefinition(source);
            jacksonConverterDef.getConstructorArgumentValues().addIndexedArgumentValue(0, jacksonFactoryDef);
            messageConverters.add(jacksonConverterDef);
        } else if (gsonPresent) {
            messageConverters.add(createConverterDefinition(GsonHttpMessageConverter.class, source));
        }
        if (jackson2SmilePresent) {
            RootBeanDefinition jacksonConverterDef = createConverterDefinition(MappingJackson2SmileHttpMessageConverter.class, source);
            GenericBeanDefinition jacksonFactoryDef = createObjectMapperFactoryDefinition(source);
            jacksonFactoryDef.getPropertyValues().add("factory", new SmileFactory());
            jacksonConverterDef.getConstructorArgumentValues().addIndexedArgumentValue(0, jacksonFactoryDef);
            messageConverters.add(jacksonConverterDef);
        }
        if (jackson2CborPresent) {
            RootBeanDefinition jacksonConverterDef = createConverterDefinition(MappingJackson2CborHttpMessageConverter.class, source);
            GenericBeanDefinition jacksonFactoryDef = createObjectMapperFactoryDefinition(source);
            jacksonFactoryDef.getPropertyValues().add("factory", new CBORFactory());
            jacksonConverterDef.getConstructorArgumentValues().addIndexedArgumentValue(0, jacksonFactoryDef);
            messageConverters.add(jacksonConverterDef);
        }
    }
    return messageConverters;
}
Also used : SourceHttpMessageConverter(org.springframework.http.converter.xml.SourceHttpMessageConverter) AtomFeedHttpMessageConverter(org.springframework.http.converter.feed.AtomFeedHttpMessageConverter) CBORFactory(com.fasterxml.jackson.dataformat.cbor.CBORFactory) Element(org.w3c.dom.Element) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) ManagedList(org.springframework.beans.factory.support.ManagedList) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) SmileFactory(com.fasterxml.jackson.dataformat.smile.SmileFactory) RssChannelHttpMessageConverter(org.springframework.http.converter.feed.RssChannelHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)

Aggregations

GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)37 Test (org.junit.Test)10 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)7 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)7 BeanCreationException (org.springframework.beans.factory.BeanCreationException)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)4 TestBean (org.springframework.tests.sample.beans.TestBean)4 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)2 Qualifier (org.springframework.beans.factory.annotation.Qualifier)2 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)2 AutowireCandidateQualifier (org.springframework.beans.factory.support.AutowireCandidateQualifier)2 ManagedList (org.springframework.beans.factory.support.ManagedList)2 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)2 Element (org.w3c.dom.Element)2 CBORFactory (com.fasterxml.jackson.dataformat.cbor.CBORFactory)1 SmileFactory (com.fasterxml.jackson.dataformat.smile.SmileFactory)1 GrailsTagLibClass (grails.core.GrailsTagLibClass)1