Search in sources :

Example 1 with BeanDefinitionCustomizer

use of org.springframework.beans.factory.config.BeanDefinitionCustomizer in project spring-framework by spring-projects.

the class AnnotatedBeanDefinitionReader method doRegisterBean.

/**
	 * Register a bean from the given bean class, deriving its metadata from
	 * class-declared annotations.
	 * @param annotatedClass the class of the bean
	 * @param instanceSupplier a callback for creating an instance of the bean
	 * (may be {@code null})
	 * @param name an explicit name for the bean
	 * @param qualifiers specific qualifier annotations to consider, if any,
	 * in addition to qualifiers at the bean class level
	 * @param definitionCustomizers one or more callbacks for customizing the
	 * factory's {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
	 * @since 5.0
	 */
<T> void doRegisterBean(Class<T> annotatedClass, Supplier<T> instanceSupplier, String name, Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
        return;
    }
    abd.setInstanceSupplier(instanceSupplier);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class == qualifier) {
                abd.setPrimary(true);
            } else if (Lazy.class == qualifier) {
                abd.setLazyInit(true);
            } else {
                abd.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }
    if (definitionCustomizers != null) {
        for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
            customizer.customize(abd);
        }
    }
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanDefinitionCustomizer(org.springframework.beans.factory.config.BeanDefinitionCustomizer) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier)

Example 2 with BeanDefinitionCustomizer

use of org.springframework.beans.factory.config.BeanDefinitionCustomizer in project spring-framework by spring-projects.

the class GenericApplicationContext method registerBean.

/**
 * Register a bean from the given bean class, using the given supplier for
 * obtaining a new instance (typically declared as a lambda expression or
 * method reference), optionally customizing its bean definition metadata
 * (again typically declared as a lambda expression).
 * <p>This method can be overridden to adapt the registration mechanism for
 * all {@code registerBean} methods (since they all delegate to this one).
 * @param beanName the name of the bean (may be {@code null})
 * @param beanClass the class of the bean
 * @param supplier a callback for creating an instance of the bean (in case
 * of {@code null}, resolving a public constructor to be autowired instead)
 * @param customizers one or more callbacks for customizing the factory's
 * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
 * @since 5.0
 */
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
    ClassDerivedBeanDefinition beanDefinition = new ClassDerivedBeanDefinition(beanClass);
    if (supplier != null) {
        beanDefinition.setInstanceSupplier(supplier);
    }
    for (BeanDefinitionCustomizer customizer : customizers) {
        customizer.customize(beanDefinition);
    }
    String nameToUse = (beanName != null ? beanName : beanClass.getName());
    registerBeanDefinition(nameToUse, beanDefinition);
}
Also used : BeanDefinitionCustomizer(org.springframework.beans.factory.config.BeanDefinitionCustomizer)

Example 3 with BeanDefinitionCustomizer

use of org.springframework.beans.factory.config.BeanDefinitionCustomizer in project spring-framework by spring-projects.

the class AnnotatedBeanDefinitionReader method doRegisterBean.

/**
 * Register a bean from the given bean class, deriving its metadata from
 * class-declared annotations.
 * @param beanClass the class of the bean
 * @param name an explicit name for the bean
 * @param qualifiers specific qualifier annotations to consider, if any,
 * in addition to qualifiers at the bean class level
 * @param supplier a callback for creating an instance of the bean
 * (may be {@code null})
 * @param customizers one or more callbacks for customizing the factory's
 * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
 * @since 5.0
 */
private <T> void doRegisterBean(Class<T> beanClass, @Nullable String name, @Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier, @Nullable BeanDefinitionCustomizer[] customizers) {
    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
        return;
    }
    abd.setInstanceSupplier(supplier);
    ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
    abd.setScope(scopeMetadata.getScopeName());
    String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
    AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
    if (qualifiers != null) {
        for (Class<? extends Annotation> qualifier : qualifiers) {
            if (Primary.class == qualifier) {
                abd.setPrimary(true);
            } else if (Lazy.class == qualifier) {
                abd.setLazyInit(true);
            } else {
                abd.addQualifier(new AutowireCandidateQualifier(qualifier));
            }
        }
    }
    if (customizers != null) {
        for (BeanDefinitionCustomizer customizer : customizers) {
            customizer.customize(abd);
        }
    }
    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
    definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanDefinitionCustomizer(org.springframework.beans.factory.config.BeanDefinitionCustomizer) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier)

Aggregations

BeanDefinitionCustomizer (org.springframework.beans.factory.config.BeanDefinitionCustomizer)3 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)2 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)2 AutowireCandidateQualifier (org.springframework.beans.factory.support.AutowireCandidateQualifier)2