如何在
Spring中获得实现特定通用接口(例如Filter< TestEvent>)的所有bean的引用?
这是我想用最少的行数实现的:
public interface Filter {
boolean approve(T event);
}
public class TestEventFilter implements Filter {
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 filters = context.getBeansOfType(Filter.class);
for(Filter filter: filters.values()){
if (!filter.approve(event)) {
return; // abort if a filter does not approve the event
}
}
//...
}
}
我当前的实现使用反射来确定filter.approve在调用之前是否接受了该事件.
例如.
Map filters = context.getBeansOfType(Filter.class);
for(Filter filter: filters.values()){
if (doesFilterAcceptEventAsArgument(filter, event)) {
if (!filter.approve(event)) {
return; // abort if a filter does not approve the event
}
}
}
在什么地方,doFilterAcceptEventAsArgument完成了我想要摆脱的所有丑陋的工作.有什么建议?