註冊 Servlet 三大組件【Servlet、Filter、Listener】
由於 Spring Boot 默認是以 jar 包的方式啓動嵌入式 Servlet 容器來啓動 Spring Boot 的 Web 應用,沒有 web.xml 文件。
註冊三大組件用以下方式:
Servlet——ServletRegistrationBean
demo.yangxu.springboot.servlet.MyServlet
package demo.yangxu.springboot.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class MyServlet extends HttpServlet {
//處理get請求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
//處理post請求
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Hello MyServlet");
}
}
demo.yangxu.springboot.config.MyServerConfig#myServlet
@Bean
public ServletRegistrationBean myServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
//設置啓動順序
registrationBean.setLoadOnStartup(1);
return registrationBean;
}
Filter——FilterRegistrationBean
demo.yangxu.springboot.filter.MyFilter
package demo.yangxu.springboot.filter;
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("MyFilter process...");
chain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
demo.yangxu.springboot.config.MyServerConfig#myFilter
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
Listener——ServletListenerRegistrationBean
demo.yangxu.springboot.listener.MyListener
package demo.yangxu.springboot.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("contextInitialized...web應用啓動");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("contextDestroyed...當前web項目銷燬");
}
}
org.springframework.boot.web.servlet.ServletListenerRegistrationBean
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
return registrationBean;
}
測試項目銷燬:
http://localhost:8083/crud/myServlet
http://localhost:8083/crud/hello
自動配置
Spring Boot 自動配置 SpringMVC 的時候,會自動註冊 SpringMVC 的前端控制器——DIspatcherServlet。
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistration
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
webMvcProperties.getServlet().getPath());
//默認攔截: / 所有請求;包括靜態資源,但是不攔截jsp請求; /*會攔截jsp
//可以通過spring.mvc.pathmatch.use-suffix-pattern=true 與
//server.servlet.path
//來修改 SpringMVC 前端控制器默認攔截的請求路徑
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
multipartConfig.ifAvailable(registration::setMultipartConfig);
return registration;
}
org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.Servlet#path
/**
* Path of the dispatcher servlet.
*/
private String path = "/";