Search in sources :

Example 1 with BeanDefinitionHolder

use of cn.taketoday.beans.factory.config.BeanDefinitionHolder in project today-infrastructure by TAKETODAY.

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(cn.taketoday.beans.factory.support.AbstractBeanDefinition) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) AbstractBeanDefinition(cn.taketoday.beans.factory.support.AbstractBeanDefinition)

Example 2 with BeanDefinitionHolder

use of cn.taketoday.beans.factory.config.BeanDefinitionHolder in project today-infrastructure by TAKETODAY.

the class AnnotationDrivenBeanDefinitionParser method registerPostProcessor.

private static void registerPostProcessor(ParserContext parserContext, BeanDefinitionBuilder builder, String beanName) {
    builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    parserContext.getRegistry().registerBeanDefinition(beanName, builder.getBeanDefinition());
    BeanDefinitionHolder holder = new BeanDefinitionHolder(builder.getBeanDefinition(), beanName);
    parserContext.registerComponent(new BeanComponentDefinition(holder));
}
Also used : BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(cn.taketoday.beans.factory.parsing.BeanComponentDefinition)

Example 3 with BeanDefinitionHolder

use of cn.taketoday.beans.factory.config.BeanDefinitionHolder in project today-infrastructure by TAKETODAY.

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(cn.taketoday.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with BeanDefinitionHolder

use of cn.taketoday.beans.factory.config.BeanDefinitionHolder in project today-infrastructure by TAKETODAY.

the class AbstractInterceptorDrivenBeanDefinitionDecorator method decorate.

@Override
public final BeanDefinitionHolder decorate(@NonNull Node node, BeanDefinitionHolder definitionHolder, ParserContext parserContext) {
    BeanDefinitionRegistry registry = parserContext.getRegistry();
    // get the root bean name - will be the name of the generated proxy factory bean
    String existingBeanName = definitionHolder.getBeanName();
    BeanDefinition targetDefinition = definitionHolder.getBeanDefinition();
    BeanDefinitionHolder targetHolder = new BeanDefinitionHolder(targetDefinition, existingBeanName + ".TARGET");
    // delegate to subclass for interceptor definition
    BeanDefinition interceptorDefinition = createInterceptorDefinition(node);
    // generate name and register the interceptor
    String interceptorName = existingBeanName + '.' + getInterceptorNameSuffix(interceptorDefinition);
    BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(interceptorDefinition, interceptorName), registry);
    BeanDefinitionHolder result = definitionHolder;
    if (!isProxyFactoryBeanDefinition(targetDefinition)) {
        // create the proxy definition
        RootBeanDefinition proxyDefinition = new RootBeanDefinition();
        // create proxy factory bean definition
        proxyDefinition.setBeanClass(ProxyFactoryBean.class);
        proxyDefinition.setScope(targetDefinition.getScope());
        proxyDefinition.setLazyInit(targetDefinition.isLazyInit());
        // set the target
        proxyDefinition.setDecoratedDefinition(targetHolder);
        proxyDefinition.getPropertyValues().add("target", targetHolder);
        // create the interceptor names list
        proxyDefinition.getPropertyValues().add("interceptorNames", new ManagedList<String>());
        // copy autowire settings from original bean definition.
        proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
        proxyDefinition.setPrimary(targetDefinition.isPrimary());
        if (targetDefinition instanceof AbstractBeanDefinition) {
            proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
        }
        // wrap it in a BeanDefinitionHolder with bean name
        result = new BeanDefinitionHolder(proxyDefinition, existingBeanName);
    }
    addInterceptorNameToList(interceptorName, result.getBeanDefinition());
    return result;
}
Also used : AbstractBeanDefinition(cn.taketoday.beans.factory.support.AbstractBeanDefinition) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) AbstractBeanDefinition(cn.taketoday.beans.factory.support.AbstractBeanDefinition)

Example 5 with BeanDefinitionHolder

use of cn.taketoday.beans.factory.config.BeanDefinitionHolder in project today-infrastructure by TAKETODAY.

the class QualifierAnnotationAutowireContextTests method autowiredMethodParameterWithStaticallyQualifiedCandidate.

@Test
public void autowiredMethodParameterWithStaticallyQualifiedCandidate() {
    GenericApplicationContext context = new GenericApplicationContext();
    ConstructorArgumentValues cavs = new ConstructorArgumentValues();
    cavs.addGenericArgumentValue(JUERGEN);
    RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
    context.registerBeanDefinition(JUERGEN, ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
    context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    context.refresh();
    QualifiedMethodParameterTestBean bean = (QualifiedMethodParameterTestBean) context.getBean("autowired");
    assertThat(bean.getPerson().getName()).isEqualTo(JUERGEN);
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Test(org.junit.jupiter.api.Test)

Aggregations

BeanDefinitionHolder (cn.taketoday.beans.factory.config.BeanDefinitionHolder)28 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)12 Test (org.junit.jupiter.api.Test)12 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)7 BeanDefinitionStoreException (cn.taketoday.beans.factory.BeanDefinitionStoreException)5 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)5 ConstructorArgumentValues (cn.taketoday.beans.factory.config.ConstructorArgumentValues)5 ScopeMetadata (cn.taketoday.context.loader.ScopeMetadata)5 BeanComponentDefinition (cn.taketoday.beans.factory.parsing.BeanComponentDefinition)4 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)4 IOException (java.io.IOException)3 AnnotatedGenericBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedGenericBeanDefinition)2 AutowireCandidateQualifier (cn.taketoday.beans.factory.support.AutowireCandidateQualifier)2 BeanDefinitionRegistry (cn.taketoday.beans.factory.support.BeanDefinitionRegistry)2 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2 MessageSource (cn.taketoday.context.MessageSource)2 StaticApplicationContext (cn.taketoday.context.support.StaticApplicationContext)2 StaticMessageSource (cn.taketoday.context.support.StaticMessageSource)2 DescriptiveResource (cn.taketoday.core.io.DescriptiveResource)2 Resource (cn.taketoday.core.io.Resource)2