相关疑难解决方法(0)

获取所有在Spring中实现通用接口的bean

如何<TestEvent在Spring中获得实现特定通用接口(例如Filter >)的所有bean的引用?

这是我想用最少的行数实现的:

public interface Filter<T extends Event> {

    boolean approve(T event);

}


public class TestEventFilter implements Filter<TestEvent> {

    public boolean approve(TestEvent event){
        return false;
    }

}

public class EventHandler{
    private ApplicationContext context;

    public void Eventhandler(DomainEvent event) {
        // I want to do something like following, but this is not valid code
        Map<String, Filter> filters = context.getBeansOfType(Filter<event.getClass()>.class);
        for(Filter filter: filters.values()){
            if (!filter.approve(event)) {
                return;  // abort if a filter does not approve the event
            }
        }
        //...
    } …
Run Code Online (Sandbox Code Playgroud)

java spring

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×1

spring ×1