Spring动态注入泛型集合Bean

我们在使用Spring框架的时候,常常需要注入相关的Bean,如果需要注入有相同类型的Bean的时候,会比较麻烦,需要一个一个注入,并且相对来说扩展性不是很好。

因此提供一种策略模式下获取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

转载于:https://my.oschina.net/guanhe/blog/1821060

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值