Search code examples
javaspringeventsspring-elspring-ioc

How do I reference the ApplicationEventPublisher in SpEL?


I am wanting to publish an application event using ApplicationEventPublisher#publishEvent() from a Spring Web Flow step. The publisher is usually the application context, but I'd rather not make that assumption unless necessary (particularly for mocking). Is there a recommended approach for referring to the publisher object from a SpEL expression?


Solution

  • You can't "inject" the publisher into a SpEL expression, like you would with a bean.

    Add a custom bean to the context that is ApplicationEventPublisherAware; add a getter and use @bean.publisher.publishEvent(...).