46.SpringBoot学习笔记–注册 Servlet 三大组件
注册 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 = "/";