如何在Spring中获得实现特定通用接口(例如Filter <TestEvent
>)的所有bean的引用?获取在Spring中实现通用接口的所有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
}
}
//...
}
}
我的当前实现使用反射来确定是否filter.approve并调用它之前接受的事件。 例如
Map<String, Filter> 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
}
}
}
凡doesFilterAcceptEventAsArgument做所有的丑恶的工作,我想希望得到远离。有什么建议么?
谢谢,就像我怀疑的一样。 – 2010-07-12 12:53:28