GenericApplicationContext offers Supplier-based registration with BeanDefinitionCustomizer callback
Issue: SPR-14832
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.config;
|
||||
|
||||
/**
|
||||
* Callback for customizing a given bean definition.
|
||||
* Designed for use with a lambda expression or method reference.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 5.0
|
||||
* @see org.springframework.beans.factory.support.BeanDefinitionBuilder#applyCustomizers
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface BeanDefinitionCustomizer {
|
||||
|
||||
/**
|
||||
* Customize the given bean definition.
|
||||
*/
|
||||
void customize(BeanDefinition bd);
|
||||
|
||||
}
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinitionCustomizer;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
@@ -41,6 +44,17 @@ public class BeanDefinitionBuilder {
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link GenericBeanDefinition}.
|
||||
* @param beanClassName the class name for the bean that the definition is being created for
|
||||
*/
|
||||
public static BeanDefinitionBuilder genericBeanDefinition(String beanClassName) {
|
||||
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
|
||||
builder.beanDefinition = new GenericBeanDefinition();
|
||||
builder.beanDefinition.setBeanClassName(beanClassName);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link GenericBeanDefinition}.
|
||||
* @param beanClass the {@code Class} of the bean that the definition is being created for
|
||||
@@ -54,33 +68,15 @@ public class BeanDefinitionBuilder {
|
||||
|
||||
/**
|
||||
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link GenericBeanDefinition}.
|
||||
* @param beanClassName the class name for the bean that the definition is being created for
|
||||
* @param beanClass the {@code Class} of the bean that the definition is being created for
|
||||
* @param instanceSupplier a callback for creating an instance of the bean
|
||||
* @since 5.0
|
||||
*/
|
||||
public static BeanDefinitionBuilder genericBeanDefinition(String beanClassName) {
|
||||
public static <T> BeanDefinitionBuilder genericBeanDefinition(Class<T> beanClass, Supplier<T> instanceSupplier) {
|
||||
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
|
||||
builder.beanDefinition = new GenericBeanDefinition();
|
||||
builder.beanDefinition.setBeanClassName(beanClassName);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link RootBeanDefinition}.
|
||||
* @param beanClass the {@code Class} of the bean that the definition is being created for
|
||||
*/
|
||||
public static BeanDefinitionBuilder rootBeanDefinition(Class<?> beanClass) {
|
||||
return rootBeanDefinition(beanClass, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link RootBeanDefinition}.
|
||||
* @param beanClass the {@code Class} of the bean that the definition is being created for
|
||||
* @param factoryMethodName the name of the method to use to construct the bean instance
|
||||
*/
|
||||
public static BeanDefinitionBuilder rootBeanDefinition(Class<?> beanClass, String factoryMethodName) {
|
||||
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
|
||||
builder.beanDefinition = new RootBeanDefinition();
|
||||
builder.beanDefinition.setBeanClass(beanClass);
|
||||
builder.beanDefinition.setFactoryMethodName(factoryMethodName);
|
||||
builder.beanDefinition.setInstanceSupplier(instanceSupplier);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -105,6 +101,27 @@ public class BeanDefinitionBuilder {
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link RootBeanDefinition}.
|
||||
* @param beanClass the {@code Class} of the bean that the definition is being created for
|
||||
*/
|
||||
public static BeanDefinitionBuilder rootBeanDefinition(Class<?> beanClass) {
|
||||
return rootBeanDefinition(beanClass, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link RootBeanDefinition}.
|
||||
* @param beanClass the {@code Class} of the bean that the definition is being created for
|
||||
* @param factoryMethodName the name of the method to use to construct the bean instance
|
||||
*/
|
||||
public static BeanDefinitionBuilder rootBeanDefinition(Class<?> beanClass, String factoryMethodName) {
|
||||
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
|
||||
builder.beanDefinition = new RootBeanDefinition();
|
||||
builder.beanDefinition.setBeanClass(beanClass);
|
||||
builder.beanDefinition.setFactoryMethodName(factoryMethodName);
|
||||
return builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link ChildBeanDefinition}.
|
||||
* @param parentName the name of the parent bean
|
||||
@@ -286,4 +303,17 @@ public class BeanDefinitionBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the given customizers to the underlying bean definition.
|
||||
* @since 5.0
|
||||
*/
|
||||
public BeanDefinitionBuilder applyCustomizers(BeanDefinitionCustomizer... customizers) {
|
||||
if (customizers != null) {
|
||||
for (BeanDefinitionCustomizer customizer : customizers) {
|
||||
customizer.customize(beanDefinition);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user