2017-07-16 113 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(); 
}