Search in sources :

Example 76 with BeanDefinitionHolder

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;
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Example 77 with BeanDefinitionHolder

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);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MessageSource(org.springframework.context.MessageSource) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.jupiter.api.Test)

Example 78 with BeanDefinitionHolder

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());
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition)

Example 79 with BeanDefinitionHolder

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;
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) Nullable(org.springframework.lang.Nullable)

Example 80 with BeanDefinitionHolder

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();
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) Test(org.junit.jupiter.api.Test)

Aggregations

BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)94 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)30 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)20 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)18 Element (org.w3c.dom.Element)18 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)16 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)16 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)12 ManagedList (org.springframework.beans.factory.support.ManagedList)11 Node (org.w3c.dom.Node)11 ArrayList (java.util.ArrayList)8 ManagedMap (org.springframework.beans.factory.support.ManagedMap)6 LinkedHashSet (java.util.LinkedHashSet)5 Test (org.junit.jupiter.api.Test)5 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)5 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)5 NamedNodeMap (org.w3c.dom.NamedNodeMap)5 NodeList (org.w3c.dom.NodeList)5 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4