2017-07-16 114 views
2

我需要一個靈活的篩選器FooEvents多個EventListeners遍及我的代碼。 我可以使用@EventListener(condition =「event.enabled」),但我的過濾器需要fooEvent的許多屬性進行分析。春季eventListener與外部條件

我希望我可以從我的應用程序上下文使用謂詞豆:

@Component 
public class FooPredicate implements Predicate<FooEvent> { 
    public boolean test(FooEvent event) {...} 
} 

... 

@EventListener(condition="${fooPredicate.test(event)}") 
public void handle(FooEvent event) { ... } 

,但我得到:

org.springframework.expression.spel.SpelEvaluationException: EL1011E: 
    Method call: Attempted to call method 
    test(org.springframework.context.PayloadApplicationEvent) on null 
    context object 

是否可以使用外部的,複雜的條件EventListerns?或者至少要定義具有複雜條件的全球聽衆並在不重複全部條件的情況下繼承他們的行爲?

回答

2

你使用了錯誤的定義,fooPredicate是你需要使用一個Spring bean「@」而不是「#」來解決它作爲一個bean。看到10.5.13 Bean references

@EventListener(condition="@fooPredicate.test(#event)") 
public void handle(FooEvent event) { 
    System.out.println(); 
}