N. Pradeep
N. Pradeep

Reputation: 303

How to add a tomcat server LifeCycleListener in Spring boot

We are trying to add a our own listener in Spring boot application like :

<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener">

How do we add this in a Spring boot application?

Upvotes: 0

Views: 3706

Answers (2)

Micha&#235;l COLL
Micha&#235;l COLL

Reputation: 1297

In the latest version of spring boot the implementation has changed.

@Component
public class MyTomcatWebServerCustomizer
        implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        // customize the factory here
    }
}

The spring boot documentation

Upvotes: 2

BeeNoisy
BeeNoisy

Reputation: 1384

Just by add this config class to your project:

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by [email protected] on 16/10/28.
 */
@Configuration
public class TomcatConfig implements EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.addContextLifecycleListeners(null);
        return factory;
    }
}

And more detail here:spring boot document

Upvotes: 3

Related Questions