Java Code Examples for org.springframework.beans.factory.config.BeanDefinitionCustomizer#customize()

The following examples show how to use org.springframework.beans.factory.config.BeanDefinitionCustomizer#customize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotatedBeanDefinitionReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register a bean from the given bean class, deriving its metadata from
 * class-declared annotations.
 * @param annotatedClass the class of the bean
 * @param name an explicit name for the bean
 * @param supplier a callback for creating an instance of the bean
 * (may be {@code null})
 * @param qualifiers specific qualifier annotations to consider, if any,
 * in addition to qualifiers at the bean class level
 * @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> annotatedClass, @Nullable String name,
		@Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
		@Nullable BeanDefinitionCustomizer[] customizers) {

	AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
	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);
}
 
Example 2
Source File: BeanDefinitionBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Apply the given customizers to the underlying bean definition.
 * @since 5.0
 */
public BeanDefinitionBuilder applyCustomizers(BeanDefinitionCustomizer... customizers) {
	for (BeanDefinitionCustomizer customizer : customizers) {
		customizer.customize(this.beanDefinition);
	}
	return this;
}
 
Example 3
Source File: AnnotatedBeanDefinitionReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
		@Nullable 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));
			}
		}
	}
	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);
}
 
Example 4
Source File: BeanDefinitionBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Apply the given customizers to the underlying bean definition.
 * @since 5.0
 */
public BeanDefinitionBuilder applyCustomizers(BeanDefinitionCustomizer... customizers) {
	for (BeanDefinitionCustomizer customizer : customizers) {
		customizer.customize(this.beanDefinition);
	}
	return this;
}
 
Example 5
Source File: GenericApplicationContext.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: GenericApplicationContext.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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 or method reference).
 * <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);
}