配置嵌入式Servlet容器
SpringBoot默认使用的是嵌入式的Servlet容器
版本也可以看到
- 1)那么如何定制和修改Serverlet容器
- 1修改Server有关的配置
public class ServerProperties {
private Integer port;
private InetAddress address;
@NestedConfigurationProperty
private final ErrorProperties error = new ErrorProperties();
private Boolean useForwardHeaders;
private String serverHeader;
private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8L);
private Duration connectionTimeout;
@NestedConfigurationProperty
private Ssl ssl;
@NestedConfigurationProperty
private final Compression compression = new Compression();
@NestedConfigurationProperty
private final Http2 http2 = new Http2();
private final ServerProperties.Servlet servlet = new ServerProperties.Servlet();
private final ServerProperties.Tomcat tomcat = new ServerProperties.Tomcat();
private final ServerProperties.Jetty jetty = new ServerProperties.Jetty();
private final ServerProperties.Undertow undertow = new ServerProperties.Undertow();
- 2、编写一个WebServerFactoryCustomizer :嵌入式的Serverlet定制器,来修改Serverlet的一些配置。
//SpringBoot1.5.x
// @Bean
// public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
// return new EmbeddedServletContainerCustomizer() {
// @Override
// public void customize(ConfigurableEmbeddedServletContainer container) {
// container.setPort(8083);
// }
// };
// }
//SpringBoot2.x.x
@Bean
public WebServerFactoryCustomizer webServerFactoryCustomizer() {
return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8083);
}
};
}
注册ServerLet三大组件(Servlet、Filter、Listener)
由于Servlet默认是用jar包来启动的嵌入式Servlet容器来启动SpringBoot,没有web.xml文件
注册三大组件用一下方式:
ServletRegistrationBean
/*MyServlet 类继承HttpServlet */
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello serverLet");
}
}
//然后把相关的
@Bean
public ServletRegistrationBean myServlet() {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet());
return registrationBean;
}
FilterRegistrationBean
//注意是javax.servlet下面的Fliter
import javax.servlet.*;
import java.io.IOException;
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("myFilter process.....");
filterChain.doFilter(servletRequest, servletResponse);
}
}
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return filterRegistrationBean;
}
ServletListenerRegistrationBean
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContextListener go");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContextEvent dead");
}
}
@Bean
public ServletListenerRegistrationBean myListener(){
ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean(new MyListener());
return servletListenerRegistrationBean;
}
SpringBoot自动帮我们注册springmvc的时候,自动注册了前端控制器,DispatchServlet
@Bean(
name = {"dispatcherServletRegistration"}
)
@ConditionalOnBean(
value = {DispatcherServlet.class},
name = {"dispatcherServlet"}
)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, this.webMvcProperties.getServlet().getPath());
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
springboot 2.0 之后设置路径的方式变为了spring.mvc.servlet.path="/";
- 2)SpringBoot是否支持其他的Serverlet容器
- 引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
就自带了tomcat,我们可以把其中的tomcat替换掉。换成jetty等。都直接可以。
可以看到springboot 2.0版本里有这些的实现的定制
再通过配置的方式,就可以自动注入这些容器了
@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);
}
}