How to get a link for all beans that implement a specific common interface (e.g. Filter <TestEvent>) in Spring?
This is what I want to achieve with a minimum number of lines:
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) {
Map<String, Filter> filters = context.getBeansOfType(Filter<event.getClass()>.class);
for(Filter filter: filters.values()){
if (!filter.approve(event)) {
return;
}
}
}
}
My current implementation uses reflection to determine if filter.approve accepts an event before it is called. For instance.
Map<String, Filter> filters = context.getBeansOfType(Filter.class);
for(Filter filter: filters.values()){
if (doesFilterAcceptEventAsArgument(filter, event)) {
if (!filter.approve(event)) {
return;
}
}
}
Where makeFilterAcceptEventAsArgument does all the ugly work I'd like to slip away with. Any suggestions?
source
share