如何动态生成bean列表
How to generate a list of beans dynamically
嗨
我正在寻找一种方法来 'simplify'/缩短我的 spring 配置。
我有这个通用服务,看起来像:
public class GenericService<T> {
private Class<T> targetClass;
public void setTargetClass(Class<T> targetClass) {this.targetClass = targetClass;}
public void doSomething() {...}
}
在我的 spring-config.xml 文件中有
<bean id="entity1Service" class="GenericService">
<property name="targetClass" value="model.Entity1" />
</bean>
<bean id="entity2Service" class="GenericService">
<property name="targetClass" value="model.Entity2" />
</bean>
...
我正在尝试建立一个工厂来为我构建所有这些服务,这样我就可以在 spring-config.xml
中编写类似的内容
<bean class="ServiceFactory">
<property name="targets">
<list>
<value>model.Entity1</value>
<value>model.Entity2</value>
</list>
</property>
</bean>
这将生成 2 个 bean(一个命名为 entity1Service,另一个命名为 entity2Service)。明白了吗?
我将如何开始?我查看了 BeanFactory(不要与 FactoryBean 混淆!)但没看到如何连接所有内容。
如果我的工厂可以扫描我的包并在找到实体时生成服务(通过注释或接口实现),那就更好了,有点像 spring-data 中的 @EnableJpaRepositories 注释对于所有 JpaRepository 接口。
感谢您提供任何见解、示例、指示...
w.
您可以尝试以编程方式进行:
public class Application {
@Autowired
private ApplicationContext applicationContext;
public void loadBeans() {
NewBean newBean = applicationContext.getAutowireCapableBeanFactory().createBean(NewBean.class);
}
}
您还应该能够在创建这些 bean 后自动装配它们。
编辑:
您可以使用 bean 中的注释来命名这些 bean class:
@Component("NewBean1")
public class NewBean1 implements GenericService<T> {
}
然后当你自动装配它时,使用@Qualifier 注释
public class BeanController {
@Autowired
@Qualifier("NewBean1")
private GenericService<T> bean;
}
我相信我已经弄清楚了并发布我的结果以供将来参考。
public class GenericBeanGenerator <T, G> implements BeanFactoryPostProcessor, BeanPostProcessor {
/**
* The type of the bean to create
*/
private Class<T> type;
/**
* The list of generic values. Can be String, Class or whatever
*/
private Iterable<G> generics;
private Map<String, G> beanNameToGeneric = new HashMap<String, G>();
public GenericBeanGenerator(Class<T> type, G[] generics) {
this.type = type;
this.generics = Arrays.asList(generics);
}
public GenericBeanGenerator(Class<T> type, Iterable<G> generics) {
this.type = type;
this.generics = generics;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// for each 'generic' value, add a bean definition to the beanFactory with the appropriate bean name
for(G generic : generics) {
String beanName = getBeanName(generic);
beanNameToGeneric.put(beanName, generic);
((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(beanName, new AnnotatedGenericBeanDefinition(type) );
}
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (type.isInstance(bean) && beanNameToGeneric.containsKey(beanName)) {
@SuppressWarnings("unchecked")
T instance = (T) bean;
initiliaze(beanName, beanNameToGeneric.get(beanName), instance);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* Convert a 'generic' value to a string in order to name the bean
*/
public String getBeanName(G generic) {
return generic.toString();
}
/**
* Initialize the bean if needed.
*/
public void initiliaze(String beanName, G generic, T instance) {
}
}
现在,如果我想要一些扩展 class 的泛型服务,例如...
class GenericService<T> {
Class<T> entityClass;
public void setEntityClass(Class<T> clazz) {
this.entityClass = clazz;
}
....
}
我可以在我的一个@Configuration bean 中有这样的东西
class Config {
@Bean
public static GenericBeanGenerator<GenericService, Class<?>> genericServiceGenerator() {
List<Class<?>> entities = Arrays.asList(A.class, B.class);
return new GenericBeanGenerator<GenericService, Class<?>>(GenericService.class, entities) {
@Override
public String getBeanName(Class<?> generic) {
return generic.getSimpleName() + "Service";
}
@Override
public void initiliaze(String beanName, Class<?> generic, GenericService instance) {
instance.setEntityClass(generic);
}
};
}
}
这将为我的实体 A 和 B 生成两个名为 AService 和 BService 的 bean。
甚至更强大:如果我的实体都实现了一个名为 Idable 的接口,如果我使用类似这样的东西来扫描我的包,我就可以生成我所有的服务 bean:
BeanDefinitionRegistry bdr = new SimpleBeanDefinitionRegistry();
ClassPathBeanDefinitionScanner s = new ClassPathBeanDefinitionScanner(bdr);
TypeFilter tf = new AssignableTypeFilter(Idable.class);
s.addIncludeFilter(tf);
s.scan("org.willix.model");
entities = bdr.getBeanDefinitionNames();
嗨
我正在寻找一种方法来 'simplify'/缩短我的 spring 配置。 我有这个通用服务,看起来像:
public class GenericService<T> {
private Class<T> targetClass;
public void setTargetClass(Class<T> targetClass) {this.targetClass = targetClass;}
public void doSomething() {...}
}
在我的 spring-config.xml 文件中有
<bean id="entity1Service" class="GenericService">
<property name="targetClass" value="model.Entity1" />
</bean>
<bean id="entity2Service" class="GenericService">
<property name="targetClass" value="model.Entity2" />
</bean>
...
我正在尝试建立一个工厂来为我构建所有这些服务,这样我就可以在 spring-config.xml
中编写类似的内容<bean class="ServiceFactory">
<property name="targets">
<list>
<value>model.Entity1</value>
<value>model.Entity2</value>
</list>
</property>
</bean>
这将生成 2 个 bean(一个命名为 entity1Service,另一个命名为 entity2Service)。明白了吗?
我将如何开始?我查看了 BeanFactory(不要与 FactoryBean 混淆!)但没看到如何连接所有内容。
如果我的工厂可以扫描我的包并在找到实体时生成服务(通过注释或接口实现),那就更好了,有点像 spring-data 中的 @EnableJpaRepositories 注释对于所有 JpaRepository 接口。
感谢您提供任何见解、示例、指示...
w.
您可以尝试以编程方式进行:
public class Application {
@Autowired
private ApplicationContext applicationContext;
public void loadBeans() {
NewBean newBean = applicationContext.getAutowireCapableBeanFactory().createBean(NewBean.class);
}
}
您还应该能够在创建这些 bean 后自动装配它们。
编辑:
您可以使用 bean 中的注释来命名这些 bean class:
@Component("NewBean1")
public class NewBean1 implements GenericService<T> {
}
然后当你自动装配它时,使用@Qualifier 注释
public class BeanController {
@Autowired
@Qualifier("NewBean1")
private GenericService<T> bean;
}
我相信我已经弄清楚了并发布我的结果以供将来参考。
public class GenericBeanGenerator <T, G> implements BeanFactoryPostProcessor, BeanPostProcessor {
/**
* The type of the bean to create
*/
private Class<T> type;
/**
* The list of generic values. Can be String, Class or whatever
*/
private Iterable<G> generics;
private Map<String, G> beanNameToGeneric = new HashMap<String, G>();
public GenericBeanGenerator(Class<T> type, G[] generics) {
this.type = type;
this.generics = Arrays.asList(generics);
}
public GenericBeanGenerator(Class<T> type, Iterable<G> generics) {
this.type = type;
this.generics = generics;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// for each 'generic' value, add a bean definition to the beanFactory with the appropriate bean name
for(G generic : generics) {
String beanName = getBeanName(generic);
beanNameToGeneric.put(beanName, generic);
((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(beanName, new AnnotatedGenericBeanDefinition(type) );
}
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (type.isInstance(bean) && beanNameToGeneric.containsKey(beanName)) {
@SuppressWarnings("unchecked")
T instance = (T) bean;
initiliaze(beanName, beanNameToGeneric.get(beanName), instance);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* Convert a 'generic' value to a string in order to name the bean
*/
public String getBeanName(G generic) {
return generic.toString();
}
/**
* Initialize the bean if needed.
*/
public void initiliaze(String beanName, G generic, T instance) {
}
}
现在,如果我想要一些扩展 class 的泛型服务,例如...
class GenericService<T> {
Class<T> entityClass;
public void setEntityClass(Class<T> clazz) {
this.entityClass = clazz;
}
....
}
我可以在我的一个@Configuration bean 中有这样的东西
class Config {
@Bean
public static GenericBeanGenerator<GenericService, Class<?>> genericServiceGenerator() {
List<Class<?>> entities = Arrays.asList(A.class, B.class);
return new GenericBeanGenerator<GenericService, Class<?>>(GenericService.class, entities) {
@Override
public String getBeanName(Class<?> generic) {
return generic.getSimpleName() + "Service";
}
@Override
public void initiliaze(String beanName, Class<?> generic, GenericService instance) {
instance.setEntityClass(generic);
}
};
}
}
这将为我的实体 A 和 B 生成两个名为 AService 和 BService 的 bean。
甚至更强大:如果我的实体都实现了一个名为 Idable 的接口,如果我使用类似这样的东西来扫描我的包,我就可以生成我所有的服务 bean:
BeanDefinitionRegistry bdr = new SimpleBeanDefinitionRegistry();
ClassPathBeanDefinitionScanner s = new ClassPathBeanDefinitionScanner(bdr);
TypeFilter tf = new AssignableTypeFilter(Idable.class);
s.addIncludeFilter(tf);
s.scan("org.willix.model");
entities = bdr.getBeanDefinitionNames();