Skip to content

Instantly share code, notes, and snippets.

@songyunlu
Created December 4, 2014 11:17
Show Gist options
  • Save songyunlu/2a0cbf27e522dd4a8f70 to your computer and use it in GitHub Desktop.
Save songyunlu/2a0cbf27e522dd4a8f70 to your computer and use it in GitHub Desktop.
package demo;
import java.util.Arrays;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.catalina.LifecycleListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.boot.context.embedded.tomcat.ServletContextInitializerLifecycleListener;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
ServletContextInitializer ctxtInitializer = new MyServletContextInitializer();
LifecycleListener listener = new ServletContextInitializerLifecycleListener(ctxtInitializer);
tomcat.setContextLifecycleListeners(Arrays.asList(listener));
return tomcat;
}
}
class MyServletContextInitializer implements ServletContextInitializer {
private static final Log log = LogFactory.getLog(MyServletContextInitializer.class);
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
log.info("the tomcat starts up");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment