标签:控制台 event servlet list 注解 initial string ext void
两种方式:
1. 编写Listener类
/*** springBoot 整合 Listener 方式一: ** ** 传统方式 一: ** <listener> ** <listener-class>com.linhw.demo.listener.FirstListener</listener-class> ** </listener> **/ @WebListener public class FirstListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent sce) { //启动时,在控制台可以看到 System.out.println("Listener...init......"); } @Override public void contextDestroyed(ServletContextEvent sce) { } }
2. 编写启动类
@SpringBootApplication @ServletComponentScan public class App5 { public static void main(String[] args) { SpringApplication.run(App5.class, args); } }
1. 编写Listener类
//与第一种方式的区别,就是没有加@WebListener注解 public class SecondListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent sce) { //启动时,在控制台可以看到 System.out.println("Method Listener...init......"); } @Override public void contextDestroyed(ServletContextEvent sce) { } }
2. 编写启动类
@SpringBootApplication public class App6 { public static void main(String[] args) { SpringApplication.run(App6.class, args); } //添加@Bean将名为"getListener"的bean加入到容器中 @Bean public ServletListenerRegistrationBean<SecondListener> getListener(){ ServletListenerRegistrationBean<SecondListener> secondLisener = new ServletListenerRegistrationBean<SecondListener>(new SecondListener()); return secondLisener; } }
标签:控制台 event servlet list 注解 initial string ext void
原文地址:https://www.cnblogs.com/myitnews/p/11518386.html