目录
一、注册Servlet三大组件Servlet/Filter/Listener
一、注册Servlet三大组件Servlet/Filter/Listener
以前 Web 应用使用外置Tomcat 容器部署,可在 web.xml 文件中注册 Servlet 三大组件。SpringBoot默认是以jar包的方式运行嵌入式Servlet容器来启动应用,没有web.xml文件,Spring提供以下Bean来注册三大组件:
- ServletRegistrationBean:注册自定义Servlet
- FilterRegistrationBean:注册自定义Filter
- ServletListenerRegistrationBean:注册自定义Listener
注册servlet组件
自定义servlet
public class HelloServlet extends HttpServlet {
@Override
//处理get请求
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("helloServlet execute...");
}
}
创建一个servlet相关配置类
@Configuration
public class MyServletConfig {
//注册servlet
@Bean
public ServletRegistrationBean helloServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new HelloServlet(), "/hello");//参数1.自定义servlet组件 参数2.url映射
//设置servlet加载顺序
bean.setLoadOnStartup(1);
return bean;
}
}
注册Filter组件
自定义filter
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter过滤完成");
//放行
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("filter销毁");
}
}
配置类
@Configuration
public class MyServletConfig {
//注册filter
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean<MyFilter> bean = new FilterRegistrationBean();
bean.setFilter(new MyFilter());
//过滤哪些请求
bean.setUrlPatterns(Arrays.asList("/hello"));
return bean;
}
注册Listener组件
自定义Listener
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Springboot.Servlet应用启动");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Springboot.Servlet应用销毁");
}
}
配置类
@Configuration
public class MyServletConfig {
//注册listener
@Bean
public ServletListenerRegistrationBean mylistener(){
return new ServletListenerRegistrationBean(new MyListener());
}
二、SpringMVC前端控制器
SpringBoot在DispatcherServletAutoConfiguration自动配置中 , 帮我们注册 SpringMVC 的前端控制器::DispatcherServletRegistration
public class DispatcherServletAutoConfiguration {
protected static class DispatcherServletRegistrationConfiguration {
protected DispatcherServletRegistrationConfiguration() {
}
@Bean(
name = {"dispatcherServletRegistration"}
)
@ConditionalOnBean(
value = {DispatcherServlet.class},
name = {"dispatcherServlet"}
)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet, WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
// getPath()='/' 拦截所有请求(包括静态资源)但不会拦截jsp请求;而 /* 会拦截jsp
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, webMvcProperties.getServlet().getPath());
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
multipartConfig.ifAvailable(registration::setMultipartConfig);
return registration;
}
}
三、修改Servlet容器配置
通过pom.xml可知,Springboot默认使用Tomcat作为嵌入式的Servlet容器,Springboot2.2版本默认使用的是Tomcat9.0.31版本容器
方式1:在application全局配置文件中,修改server开头有关的配置,ServerProperties可查看属性名
方式2:使用定制器修改Servlet容器配置
在2.x版本通过实现WebServerFactoryCustomizer接口的customize方法,来修改Servlet容器的配置
//定义为配置类
@Configuration
public class MyServletConfig {
@Bean
public WebServerFactoryCustomizer webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer(){
@Override
public void customize(WebServerFactory factory) {
ConfigurableServletWebServerFactory serverFactory = (ConfigurableServletWebServerFactory)factory;
//设置端口号,如果application配置文件和定制器的配置冲突,默认使用定制器的配置
serverFactory.setPort(8082);
serverFactory.setContextPath("/servlet2");
}
};
}
}
四、切换其他嵌入式Servlet容器
SpringBoot 默认针对Servlet容器提供以下支持:
- Tomcat(默认使用)
- Jetty:支持长连接项目(如:聊天页面)
- Undertow : 不支持 JSP , 但是并发性能高,是高性能非阻塞的容器
在pom.xml文件移除tomcat依赖,添加其他Servlet容器依赖就可以了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--移除tomcat依赖-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--添加jetty依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>