use of org.springframework.beans.factory.config.BeanDefinitionHolder in project redisson by redisson.
the class RedissonNamespaceParserSupport method registerBeanDefinition.
public BeanComponentDefinition registerBeanDefinition(BeanDefinitionBuilder builder, String id, String[] aliases, ParserContext parserContext) {
BeanDefinitionHolder holder = new BeanDefinitionHolder(builder.getBeanDefinition(), id, aliases);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
BeanComponentDefinition componentDefinition = new BeanComponentDefinition(holder);
parserContext.registerComponent(componentDefinition);
return componentDefinition;
}
use of org.springframework.beans.factory.config.BeanDefinitionHolder in project spring-framework by spring-projects.
the class AutoProxyCreatorTests method testBeanNameAutoProxyCreator.
@Test
public void testBeanNameAutoProxyCreator() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testInterceptor", TestInterceptor.class);
RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class);
proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor");
proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,innerBean,singletonFactoryToBeProxied");
sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition innerBean = new RootBeanDefinition(TestBean.class);
bd.getPropertyValues().add("spouse", new BeanDefinitionHolder(innerBean, "innerBean"));
sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);
sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
sac.registerSingleton("autowiredIndexedTestBean", IndexedTestBean.class);
sac.refresh();
MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
assertThat(Proxy.isProxyClass(messageSource.getClass())).isFalse();
assertThat(Proxy.isProxyClass(singletonToBeProxied.getClass())).isTrue();
assertThat(Proxy.isProxyClass(singletonToBeProxied.getSpouse().getClass())).isTrue();
// test whether autowiring succeeded with auto proxy creation
assertThat(singletonToBeProxied.getNestedIndexedBean()).isEqualTo(sac.getBean("autowiredIndexedTestBean"));
TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor");
// already 2: getSpouse + getNestedIndexedBean calls above
assertThat(ti.nrOfInvocations).isEqualTo(2);
singletonToBeProxied.getName();
singletonToBeProxied.getSpouse().getName();
assertThat(ti.nrOfInvocations).isEqualTo(5);
ITestBean tb = (ITestBean) sac.getBean("singletonFactoryToBeProxied");
assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
assertThat(ti.nrOfInvocations).isEqualTo(5);
tb.getAge();
assertThat(ti.nrOfInvocations).isEqualTo(6);
ITestBean tb2 = (ITestBean) sac.getBean("singletonFactoryToBeProxied");
assertThat(tb2).isSameAs(tb);
assertThat(ti.nrOfInvocations).isEqualTo(6);
tb2.getAge();
assertThat(ti.nrOfInvocations).isEqualTo(7);
}
use of org.springframework.beans.factory.config.BeanDefinitionHolder in project spring-framework by spring-projects.
the class ScopedProxyUtils method createScopedProxy.
/**
* Generate a scoped proxy for the supplied target bean, registering the target
* bean with an internal name and setting 'targetBeanName' on the scoped proxy.
* @param definition the original bean definition
* @param registry the bean definition registry
* @param proxyTargetClass whether to create a target class proxy
* @return the scoped proxy definition
* @see #getTargetBeanName(String)
* @see #getOriginalBeanName(String)
*/
public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition, BeanDefinitionRegistry registry, boolean proxyTargetClass) {
String originalBeanName = definition.getBeanName();
BeanDefinition targetDefinition = definition.getBeanDefinition();
String targetBeanName = getTargetBeanName(originalBeanName);
// Create a scoped proxy definition for the original bean name,
// "hiding" the target bean in an internal target definition.
RootBeanDefinition proxyDefinition = new RootBeanDefinition(ScopedProxyFactoryBean.class);
proxyDefinition.setDecoratedDefinition(new BeanDefinitionHolder(targetDefinition, targetBeanName));
proxyDefinition.setOriginatingBeanDefinition(targetDefinition);
proxyDefinition.setSource(definition.getSource());
proxyDefinition.setRole(targetDefinition.getRole());
proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName);
if (proxyTargetClass) {
targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
// ScopedProxyFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here.
} else {
proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE);
}
// Copy autowire settings from original bean definition.
proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
proxyDefinition.setPrimary(targetDefinition.isPrimary());
if (targetDefinition instanceof AbstractBeanDefinition) {
proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
}
// The target bean should be ignored in favor of the scoped proxy.
targetDefinition.setAutowireCandidate(false);
targetDefinition.setPrimary(false);
// Register the target bean as separate bean in the factory.
registry.registerBeanDefinition(targetBeanName, targetDefinition);
// (potentially an inner bean).
return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases());
}
use of org.springframework.beans.factory.config.BeanDefinitionHolder in project spring-framework by spring-projects.
the class AbstractBeanDefinitionParser method parse.
@Override
@Nullable
public final BeanDefinition parse(Element element, ParserContext parserContext) {
AbstractBeanDefinition definition = parseInternal(element, parserContext);
if (definition != null && !parserContext.isNested()) {
try {
String id = resolveId(element, definition, parserContext);
if (!StringUtils.hasText(id)) {
parserContext.getReaderContext().error("Id is required for element '" + parserContext.getDelegate().getLocalName(element) + "' when used as a top-level tag", element);
}
String[] aliases = null;
if (shouldParseNameAsAliases()) {
String name = element.getAttribute(NAME_ATTRIBUTE);
if (StringUtils.hasLength(name)) {
aliases = StringUtils.trimArrayElements(StringUtils.commaDelimitedListToStringArray(name));
}
}
BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, id, aliases);
registerBeanDefinition(holder, parserContext.getRegistry());
if (shouldFireEvents()) {
BeanComponentDefinition componentDefinition = new BeanComponentDefinition(holder);
postProcessComponentDefinition(componentDefinition);
parserContext.registerComponent(componentDefinition);
}
} catch (BeanDefinitionStoreException ex) {
String msg = ex.getMessage();
parserContext.getReaderContext().error((msg != null ? msg : ex.toString()), element);
return null;
}
}
return definition;
}
use of org.springframework.beans.factory.config.BeanDefinitionHolder in project spring-framework by spring-projects.
the class BeanDefinitionTests method beanDefinitionHolderEquality.
@Test
public void beanDefinitionHolderEquality() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setAbstract(true);
bd.setLazyInit(true);
bd.setScope("request");
BeanDefinitionHolder holder = new BeanDefinitionHolder(bd, "bd");
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
boolean condition1 = !bd.equals(otherBd);
assertThat(condition1).isTrue();
boolean condition = !otherBd.equals(bd);
assertThat(condition).isTrue();
otherBd.setAbstract(true);
otherBd.setLazyInit(true);
otherBd.setScope("request");
BeanDefinitionHolder otherHolder = new BeanDefinitionHolder(bd, "bd");
assertThat(holder.equals(otherHolder)).isTrue();
assertThat(otherHolder.equals(holder)).isTrue();
assertThat(holder.hashCode() == otherHolder.hashCode()).isTrue();
}
Aggregations