Spring动态注入泛型集合Bean
2018-05-30 11:17
615 查看
我们在使用Spring框架的时候,常常需要注入相关的Bean,如果需要注入有相同类型的Bean的时候,会比较麻烦,需要一个一个注入,并且相对来说扩展性不是很好。
因此提供一种策略模式下获取Bean的方式。
使用场景
最终使用
此处可用作适配器,用来串联起整个标准化的接口业务
也可用Map<String,Interface> beanMap 来自动加载interface的Bean,不过要注意的是beanMap中的key是BeanName
因此提供一种策略模式下获取Bean的方式。
public interface FactoryList<E extends MatchingBean<K>, K> extends List<E> { E getBean(K factor); List<E> getBeanList(K factor); } public interface MatchingBean<K> { boolean matching(K factor); }
public class FactoryListEditor extends CustomCollectionEditor { public FactoryListEditor() { super(FactoryArrayList.class); this.addPropertyChangeListener(new InitializingBeanListener()); } private class InitializingBeanListener implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent evt) { Object value = ((FactoryListEditor) evt.getSource()).getValue(); if (value instanceof InitializingBean) { try { ((InitializingBean) value).afterPropertiesSet(); } catch (Exception e) { throw new RuntimeException("初始化bean afterPropertiesSet异常", e); } } } } }
public class FactoryArrayList<E extends MatchingBean<K>, K> extends ArrayList<E> implements FactoryList<E, K>, InitializingBean, Serializable { public FactoryArrayList() { super(); } public FactoryArrayList(int size) { super(size); } public E getBean(K factor) { Iterator<E> itr = iterator(); while (itr.hasNext()) { E beanMatch = itr.next(); if (beanMatch.matching(factor)) { return beanMatch; } } return null; } public List<E> getBeanList(K factor) { Iterator<E> itr = iterator(); while (itr.hasNext()) { E beanMatch = itr.next(); if (!beanMatch.matching(factor)) { itr.remove(); } } return this; } public void afterPropertiesSet() throws Exception { if (!isEmpty()) { Object[] a = toArray(); OrderComparator.sort(a); ListIterator i = listIterator(); for (int j = 0; j < a.length; j++) { i.next(); i.set(a[j]); } } } }
使用场景
public interface IProcessor<F> extends MatchingBean<F> { public boolean go(); public InterfaceEnum getFunction(); }
public abstract class AbstractProcessor implements IProcessor<InterfaceEnum> { @Override public boolean matching(InterfaceEnum func) { return this.getFunction() == func; } }
@Service public class ChildProcessor extends AbstractProcessor { @Override public boolean go() { XX } @Override public InterfaceEnum getFunction() { return InterfaceEnum.XXX; } }
最终使用
@Resource private FactoryList<IProcessor<InterfaceEnum>, InterfaceEnum> processors;
private IProcessor getProcessor(InterfaceEnum enum) { IProcessor p = processors.getBean(enum); //没有则返回null return p; }
此处可用作适配器,用来串联起整个标准化的接口业务
也可用Map<String,Interface> beanMap 来自动加载interface的Bean,不过要注意的是beanMap中的key是BeanName
相关文章推荐
- 101. Spring Boot轻松理解动态注入,删除bean【从零开始学Spring Boot】
- spring动态注入bean
- Spring中如何动态注入Bean实例教程
- 基于Spring容器Bean的动态注入
- Spring入门之bean属性注入集合
- Spring中给Bean注入集合
- spring(基础六) spring中Bean属性为集合的注入方式
- [Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
- 动态注入Spring中的Bean实例快速指南
- [Java] Spring 3.0 /id.vs.name/简单属性的注入/bean中的scope属性/集合注入/自动装配/生命周期/
- Spring 动态注入javaBean
- Spring项目获取注入bean集合
- Spring 动态注入javaBean
- 7.5.8: Spring容器中的Bean---注入集合值
- spring 第2天,bean作用域,自动注入集合
- Spring在Bean中注入集合
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
- Spring集合类型的bean及其注入方法
- Spring中如何动态注入Bean实例教程
- Spring动态的注入Bean对象的实现方式,