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);
}
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations