1、說明java
Web 開發使用 Controller 基本上能夠完成大部分需求,可是咱們還可能會用到 Servlet、 Filter、Listener 等等web
2、在spring boot中的三種實現方式spring
2.一、代碼app
CustomServlet.java:ide
package com.example.demo.utils.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 自定義 servlet * */ public class CustomServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet get method"); doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servlet post method"); response.getWriter().write("hello world"); } }
CustomFilter.java:post
package com.example.demo.utils.filter; import javax.servlet.*; import java.io.IOException; /** * 自定義 filter * Created by DELL on 2017/6/17. */ public class CustomFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("init filter"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("do filter"); chain.doFilter(request, response); } @Override public void destroy() { System.out.println("destroy filter"); } }
CustomListener.java:url
package com.example.demo.utils.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * 自定義 listener * Created by DELL on 2017/6/17. */ public class CustomListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("contextInitialized"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("contextDestroyed"); } }
2.二、方式一:經過註冊ServletRegistrationBean、FilterRegistrationBean和ServletListenerRegistrationBeanspa
2.2.一、註冊ServletRegistrationBeanorm
Application中註冊bean:blog
/**註冊CustomServlet*/ @Bean public ServletRegistrationBean servletRegistrationBean() { return new ServletRegistrationBean(new CustomServlet(), "/roncoo"); }
2.2.二、註冊FilterRegistrationBean
Application中註冊bean:
/**註冊CustomFilter*/ @Bean public FilterRegistrationBean filterRegistrationBean() { return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean()); }
2.2.三、註冊ServletListenerRegistrationBean
Application中註冊bean:
/**註冊CustomListener*/ @Bean public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() { return new ServletListenerRegistrationBean<CustomListener>(new CustomListener()); }
上面全部例子效果:
項目啓動:
訪問:http://localhost:8080/roncoo
2.三、方式二:經過實現 ServletContextInitializer 接口直接註冊
Application實現 ServletContextInitializer 接口:
package com.example.demo; import com.example.demo.utils.filter.CustomFilter; import com.example.demo.utils.listener.CustomListener; import com.example.demo.utils.servlet.CustomServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import javax.servlet.DispatcherType; import javax.servlet.ServletContext; import javax.servlet.ServletException; import java.util.EnumSet; @SpringBootApplication public class SpringbootDemo27Application implements ServletContextInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/roncoo"); servletContext.addFilter("customFilter", new CustomFilter()).addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "customServlet"); servletContext.addListener(new CustomListener()); } public static void main(String[] args) { SpringApplication.run(SpringbootDemo27Application.class, args); } }
2.四、方式三:
在SpringBootApplication上使用@ServletComponentScan註解後,直接經過@WebServlet、@WebFilter、@WebListener註解自動註冊
@ServletComponentScan @SpringBootApplication public class SpringbootDemo27Application { //... } @WebServlet(urlPatterns = "/roncoo", name = "customServlet") public class CustomServlet extends HttpServlet { //... } @WebFilter(urlPatterns = "/*") public class CustomFilter implements Filter { //... } @WebListener public class CustomListener implements ServletContextListener { //... }