How to register a listener in Spring without a deployment descriptor

I am slowly entering the Java world through the Spring Framework (4.1.0) and need your help with Listeners.

I created an application without a deployment descriptor, all associated Spring configuration is managed in @Configuration annotated classes. Everything works, but I can’t find a way to register listeners.

Question How can I register listeners in Spring (4.1.0) based on Java @Configuration annotated classes?

+5
source share
1 answer

You do this using the WebApplicationInitializer class. @Configuration is for Spring configurations, not a deployment descriptor.

 public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { container.addListener(...); ... } } 

You can find further clarifications here: How to use Spring WebApplicationInitializer .

+7
source

Source: https://habr.com/ru/post/1202479/


All Articles