Spring Boot学习系列(九)------SpringBoot中Servlet容器的配置
Spring Boot学习系列(九)------SpringBoot中Servlet容器的配置前言在了解并使用了SpringBoot以后,我们可以快速的开发上手,SpringBoot默认打包方式为jar包,且可以自启动,就是因为它内嵌了Servlet容器,在以前使用SSM的时候,我们可以根据自己的需求来定制容器的相关参数,那么在SpringBoot应用中我们要如何配置Servlet容器呢?正...
Spring Boot学习系列(九)------SpringBoot中Servlet容器的配置
前言
在了解并使用了SpringBoot以后,我们可以快速的开发上手,SpringBoot默认打包方式为jar包,且可以自启动,就是因为它内嵌了Servlet容器,在以前使用SSM的时候,我们可以根据自己的需求来定制容器的相关参数,那么在SpringBoot应用中我们要如何配置Servlet容器呢?
正文
1.配置和修改Servlet容器的相关配置
-
在配置文件中用
server
属性来改变容器中对应的值server.port=8081 server.context‐path=/crud server.tomcat.uri‐encoding=UTF‐8 //通用的Servlet容器设置 server.xxx //Tomcat的设置 server.tomcat.xxx
-
编写一个
EmbeddedServletContainerCustomizer
,它是嵌入式servlet容器的定制器,可以用它来修改servlet的配置.在SpringBoot2.0以上版本中,EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer代替,因此要使用下面这种方式来实现:
@Bean public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryWebServerFactoryCustomizer(){ return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() { @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(8002); } }; }
2.注册servlet三大组件
在以前的项目开发中,因为一般web引用都是war包的方式,我们可以在web.xml文件中定义自己需要的servlet,filter,listener组件,在SpringBoot中,我们的打包方式都是jar,所以没有web.xml,因此在SpringBoot使用servet的容器组装要按照下面的方式来:
-
ServletRegistrationBean: 该bean用来注册一个servlet
首先直接定义一个servlet:
public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); resp.getWriter().write("自己定义的servlet!!!"); } }
@Bean public ServletRegistrationBean myServlet(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet"); return registrationBean; }
ServletRegistrationBean的有参构造函数中,第一个参数可以传入一个servlet,第二个参数可以设置映射的路径,这里我们使用
/myServlet
,项目启动以后,我们访问http://localhost:8080/myServlet
,会在页面打印对应的信息: -
FilterRegistrationBean : 该Bean用来注册一个filter
public class MyFilter implements Filter { //注册 @Override public void init(FilterConfig filterConfig) throws ServletException { } //处理请求 @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("过滤器执行了....."); filterChain.doFilter(servletRequest, servletResponse); } //销毁 @Override public void destroy() { } }
接着我们在配置类中添加我们的这个过滤器:
@Bean public FilterRegistrationBean myFilterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean( new MyFilter() ); //设置要拦截的请求路径,只要改路径的请求才会被拦截.这里可以配置多个路径,组成一个集合 filterRegistrationBean.setUrlPatterns(Arrays.asList("/myFilter")); return filterRegistrationBean; }
FilterRegistrationBean的构造方法中可以传入一个filter,在这里我们传入了自己的filter,这样在访问项目时,控制台会打印我们输出的语句:
-
ServletListenerRegistrationBean : 该Bean用来注册一个监听器listenter
public class MyListener implements ServletContextListener { //启动 @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("web应用启动了..."); } //销毁 @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("web应用销毁了..."); } }
接着在配置类里面注入这个bean:
@Bean public ServletListenerRegistrationBean myServletListenerRegistrationBean() { ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean<MyListener>(new MyListener()); return servletListenerRegistrationBean; }
这样,在我们启动和关闭项目的时候,就可以看到监听器已经生效:
SpringBoot在启动时,会为我们自动添加很多的配置,可以查看DispatcherServletAutoConfiguration
这个类,在这个类中,SpringBoot为我们注入了servlet等组件:
@Bean(
name = {"dispatcherServletRegistration"}
)
@ConditionalOnBean(
value = {DispatcherServlet.class},
name = {"dispatcherServlet"}
)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.serverProperties.getServlet().getPath());
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
}
查看这个方法,我们可以看到,在底层,SpringBoot设置的servlet路径是/
,/
默认拦截所有的请求,包括静态资源,但不会拦截jsp,而/*
会拦截jsp,我们可以通过server.servletPath
来修改SpringMVC前端控制器默认拦截的请求路径.
3. 更换其他的servlet容器
我们知道,SpringBoot默认使用的嵌入式servlet容器是tomcat,这个我们可以通过pom文件的依赖图看到:
在前面,我们说到可以通过编写一个WebServerFactoryCustomizer
来改变servlet容器的配置,我们查看这个类:
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties({ServerProperties.class})
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {
public EmbeddedWebServerFactoryCustomizerAutoConfiguration() {
}
@Configuration
@ConditionalOnClass({Undertow.class, SslClientAuthMode.class})
public static class UndertowWebServerFactoryCustomizerConfiguration {
public UndertowWebServerFactoryCustomizerConfiguration() {
}
@Bean
public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) {
return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration
@ConditionalOnClass({Server.class, Loader.class, WebAppContext.class})
public static class JettyWebServerFactoryCustomizerConfiguration {
public JettyWebServerFactoryCustomizerConfiguration() {
}
@Bean
public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) {
return new JettyWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration
@ConditionalOnClass({Tomcat.class, UpgradeProtocol.class})
public static class TomcatWebServerFactoryCustomizerConfiguration {
public TomcatWebServerFactoryCustomizerConfiguration() {
}
@Bean
public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) {
return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
}
}
我们可以看到,SpringBoot支持Tomcat,Jetty以及Undertow这三种Servlet容器,这三种容器的区别如下:
- tomcat
- Jetty 适合长连接
- Undertow 不支持jsp
现在我们想要切换内置的servlet容器,只要修改项目的pom.xml文件即可:
<!-- 使用tomcat,所以我们要切换其他的容器,要排除tomcat容器的依赖,然后引入其他的容器坐标 -->
<!‐‐ 引入web模块 ‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring‐boot‐starter‐tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!‐‐引入其他的Servlet容器‐‐>
<dependency>
<artifactId>spring‐boot‐starter‐jetty</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
<!-- 引入Undertow -->
<!‐‐ 引入web模块 ‐‐>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring‐boot‐starter‐tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!‐‐引入其他的Servlet容器‐‐>
<dependency>
<artifactId>spring‐boot‐starter‐undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
4.嵌入式Servlet容器自动配置原理
5.嵌入式Servlet容器启动原理
总结
SpringBoot内置的Servlet容器的使用就说到这里,这里还有留下关于嵌入式servlet容器的启动原理,以及自动配置的原理,因为SpringBoot升级到2.0的原因,启动的原理和原来有所有不同,在后面我会补上~~~
更多推荐
所有评论(0)