2017-07-16 15 views
2

私のコード全体に複数のEventListenersのための柔軟なフィルタがFooEvents必要です。 @EventListener(condition = "event.enabled")を使用することはできますが、私のフィルタは解析するfooEventの多くの属性を必要とします。外部条件付きのspring 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はスプリングビーンであるため、 '#'の代わりに '@'を使用してBeanとして解決する必要があります。 10.5.13 Bean references

@EventListener(condition="@fooPredicate.test(#event)") 
public void handle(FooEvent event) { 
    System.out.println(); 
} 
関連する問題