Get all beans implementing a generic interface in Spring

后端 未结 2 753
无人共我
无人共我 2020-12-19 09:21

How do I get a reference of all beans implementing a specific generic interface (e.g. Filter>) in Spring?

This is what I want to achieve wi

2条回答
  •  借酒劲吻你
    2020-12-19 09:34

    Just for reference, the simplest solution I could construct was this:

        Map filters = context.getBeansOfType(Filter.class);
        for(Filter filter: filters.values()){
            try {
                if (!filter.approve(event)) {
                    return;  // abort if a filter does not approve the event.
                }
            } catch (ClassCastException ignored){ }
        }
    

    And it worked quite well for prototyping.

提交回复
热议问题