//一定要将返回值放入容器中才会生效
@Bean
public WebServerFactoryCustomizer webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {
//定制嵌入式的Servlet容器相关的规则
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8896);
}
};
}
Spring Boot2.0以上版本WebServerFactoryCustomizer进行WebServer的个性化配置
创建一个MyServlet类:`
public class MyServlet extends HttpServlet
{
//处理get请求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Hello MyServlet");
}
//处理psot请求
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
}
在MyServletConfig配置类中注册该Servlet:
//注册三大组件
//注册Servlet到容器中
@Bean
public ServletRegistrationBean myServlet()
{
ServletRegistrationBean servletRegistrationBean =
//第一个参数:注册哪一个Servlet
//第二个参数:这个Servlet映射哪些路径
new ServletRegistrationBean(new MyServlet(),"/myServlet");
return servletRegistrationBean;
}
创建一个MyFilter类:`
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化中....");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("放行");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("销毁中....");
}
}
在MyServletConfig配置类中注册该Filter:
//注册Filter到容器中
@Bean
public FilterRegistrationBean myFilter()
{
FilterRegistrationBean registrationBean=new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
//拦截的路径
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
创建一个MyListener类:
public class MyListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("web应用启动中.....");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("当前web项目销毁");
}
}
在MyServletConfig配置类中注册该Listener:
//注册Listener到容器中
@Bean
public ServletListenerRegistrationBean myListener()
{
ServletListenerRegistrationBean<MyListener> listener = new ServletListenerRegistrationBean<>(new MyListener());
return listener;
}
@Configuration
public class MyServletConfig
{
//注册三大组件
//注册Servlet到容器中
@Bean
public ServletRegistrationBean myServlet()
{
ServletRegistrationBean servletRegistrationBean =
//第一个参数:注册哪一个Servlet
//第二个参数:这个Servlet映射哪些路径
new ServletRegistrationBean(new MyServlet(),"/myServlet");
return servletRegistrationBean;
}
//注册Filter到容器中
@Bean
public FilterRegistrationBean myFilter()
{
FilterRegistrationBean registrationBean=new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
return registrationBean;
}
//注册Listener到容器中
@Bean
public ServletListenerRegistrationBean myListener()
{
ServletListenerRegistrationBean<MyListener> listener = new ServletListenerRegistrationBean<>(new MyListener());
return listener;
}
//定制嵌入式的Servlet容器相关的规则
@Bean
public WebServerFactoryCustomizer webServerFactoryCustomizer(){
return new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() {
//定制嵌入式的Servlet容器相关的规则
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8896);
}
};
}
}
SpringBoot默认使用的是Tomcat作为嵌入式的Servlet容器:引入web模块默认就是使用嵌入式的Tomcat作为Servlet容器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>
如果想切换为其他类型的嵌入式Servlet容器,则需要先将嵌入式的Tomcat容器排除,再添加相应Servlet容器的依赖,比如想切换为Jetty
<!‐‐ 引入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>
<!‐‐引入Jetty的依赖‐‐>
<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>
<!‐‐引入Jetty的依赖‐‐>
<dependency>
<artifactId>spring‐boot‐starter‐undertow</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
如果我们在配置文件中修改Servlet的相关属性,与该配置文件绑定的ServerProperties也同样是一个定制器,因此后置处理器会也会调用,然后完成属性赋值
SpringBoot在容器中放了一个嵌入式容器工厂组件----》嵌入式容器工厂组件创建对象—》后置处理器工作—》在嵌入式容器工厂组件属性没有被赋值前,后置处理器获取所有定制器,获取相关值,赋值对嵌入式容器工厂的对应属性
SpringBoot——嵌入式Servlet容器自动配置原理以及启动原理