Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register listener in Spring without deployment descriptor

I'm slowly stepping in Java world through Spring Framework (4.1.0) and need your help with Listeners.

I have created application without deployment descriptor, all Spring related configuration is managed in @Configuration annotated classes. Everything is working but I can't find a way to register listeners.

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

like image 458
xyz Avatar asked Sep 13 '14 10:09

xyz


1 Answers

You do that with the help of the WebApplicationInitializer class. @Configuration is for Spring configurations, not for the deployment descriptor.

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

You can find more explanations here: How to use Spring's WebApplicationInitializer.

like image 182
Bogdan Avatar answered Sep 28 '22 23:09

Bogdan