SpringBoot學習4:springboot整合listener
阿新 • • 發佈:2019-02-05
frame configure 控制 zed con toc oot javax servle
整合方式一:通過註解掃描完成 Listener 組件的註冊
1、編寫listener
package com.bjsxt.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * Created by Administrator on 2019/2/5. */ @WebListener public class FirstListener implementsServletContextListener{ @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("FirstListener初始化......"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } }
2、編寫啟動類
package com.bjsxt;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; /** * Created by Administrator on 2019/2/4. */ @SpringBootApplication @ServletComponentScan //在springboot啟動時,會掃描@WebServlet、@WebFilter、@WebListener等,並將該類實例化public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
整合方式二:通過方法完成 Listener 組件註冊
1、編寫listener
package com.bjsxt.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * Created by Administrator on 2019/2/5. */ public class SecondListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("SecondListener初始化......"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { } }
2、編寫啟動類
package com.bjsxt; import com.bjsxt.filter.SecondFilter; import com.bjsxt.listener.SecondListener; import com.bjsxt.servlet.SecondServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; /** * Created by Administrator on 2019/2/4. */ @SpringBootApplication public class App2 { public static void main(String[] args){ SpringApplication.run(App2.class,args); } /** * 註冊Servlet * @return */ @Bean public ServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet()); bean.addUrlMappings("/second"); return bean; } /** * 註冊Filter * @return */ @Bean public FilterRegistrationBean getFilterRegistrationBean(){ FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter()); bean.addUrlPatterns(new String[]{"/second"}); return bean; } /** * 註冊Listener * @return */ @Bean public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){ ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener()); return bean; } }
運行啟動類,監聽器初始化輸出的信息就會在控制臺輸出
SpringBoot學習4:springboot整合listener