SpringBoot起飞系列-配置嵌入式Servlet容器(八)

一、前言

springboot中默认使用的是tomcat容器,也叫做嵌入式的servlet容器。因为它和我们平常使用的tomcat容器不一样,这个tomcat直接嵌入到的springboot,平常我们使用tomcat容器是一个独立的应用,配置的时候需要在tomcat中的xml中配置,而使用springboot就不一样了,本文我们来学习怎么配置和替换servlet容器、以及注册传统的servlet组件。

二、自定义Servlet容器的相关配置

2.1 修改配置文件的值

我们可以在application.properties中按照传统的配置方式设置,如下:

server.port=8081
server.context-path=/crud

server.tomcat.uri-encoding=UTF-8

//通用的Servlet容器设置
server.xxx
//Tomcat的设置
server.tomcat.xxx

其中server开头的是web容器统一的配置,因为我们以后可以使用其他的web容器,带tomcat的是特定为tomcat容器的配置。

2.2 编码方式配置

编写一个EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定制器,来修改Servlet容器的配置。我们可以添加一个MyServerConfig类,用注解的方式来配置我们的Server配置。

 1 package com.example.demo.config;
 2
 3 import org.springframework.boot.web.server.ConfigurableWebServerFactory;
 4 import org.springframework.boot.web.server.WebServerFactoryCustomizer;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7
 8
 9 @Configuration
10 public class MyServerConfig {
11
12     @Bean
13     public WebServerFactoryCustomizer embeddedServletContainerCustomizer(){
14         return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
15             @Override
16             public void customize(ConfigurableWebServerFactory factory) {
17                 factory.setPort(8085);
18             }
19         };
20     }
21 }

启动之后我们就把端口设置到了8085。

三、注册Servlet三大组件(Servlet、Filter、Listener)

由于springboot默认是以jar包的方式启动嵌入式的web服务器来启动springboot应用,没有web.xml文件。传统情况下我们配置三大组件都是在web.xml中添加配置,现在就不能这么做了。

3.1 注册Servlet

我们使用ServletRegistrationBean类来往容器中添加Servlet组件,在MyServerConfig中添加一个方法:

1     @Bean
2     public ServletRegistrationBean myServlet(){
3         ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
4         return registrationBean;
5     }

添加一个MyServlet类来处理/myServlet的请求:

1 public class MyServlet extends HttpServlet {
2     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
3         response.getWriter().write("hello,my servlet");
4     }
5
6     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
7         this.doPost(request, response);
8     }
9 }

启动后浏览器访问http://localhost:8085/myServlet,被我们的MyServlet接管处理,因为配置的时候我们要处理的urlPatterns是/myServlet。

3.2 注册Filter

一样的原理,我们使用FilterRegistrationBean类来往容器中添加Filter组件:

1     @Bean
2     public FilterRegistrationBean myFilter(){
3         FilterRegistrationBean registrationBean = new FilterRegistrationBean();
4         registrationBean.setFilter(new MyFilter());
5         registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
6         return registrationBean;
7     }

添加一个MyFilter的类来处理映射的url:

 1 @WebFilter(filterName = "MyFilter")
 2 public class MyFilter implements Filter {
 3     public void destroy() {
 4     }
 5
 6     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
 7         resp.getWriter().write("hello,my filter");
 8         chain.doFilter(req, resp);
 9     }
10
11     public void init(FilterConfig config) throws ServletException {
12
13     }
14
15 }

3.3 注册Listener

使用ServletListenerRegistrationBean类来往容器中添加Listener组件:

1     @Bean
2     public ServletListenerRegistrationBean myListener(){
3         ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
4         return registrationBean;
5     }

添加一个MyListenner的类来处理逻辑:

 1 public class MyListener implements ServletContextListener {
 2     @Override
 3     public void contextInitialized(ServletContextEvent sce) {
 4         System.out.println("contextInitialized...web应用启动");
 5     }
 6
 7     @Override
 8     public void contextDestroyed(ServletContextEvent sce) {
 9         System.out.println("contextDestroyed...当前web项目销毁");
10     }
11 }

这就是我们注册传统三大组件的方法。

有了以上的基础,我们就可以看一下,其实springboot为我们启动springmvc的时候也是通过这种方式注册了DispatcherServlet,这就是我们springmvc中核心的servlet。

 1 @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
 2 @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
 3 public ServletRegistrationBean dispatcherServletRegistration(
 4       DispatcherServlet dispatcherServlet) {
 5    ServletRegistrationBean registration = new ServletRegistrationBean(
 6          dispatcherServlet, this.serverProperties.getServletMapping());
 7     //默认拦截: /  所有请求;包静态资源,但是不拦截jsp请求;   /*会拦截jsp
 8     //可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径
 9
10    registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
11    registration.setLoadOnStartup(
12          this.webMvcProperties.getServlet().getLoadOnStartup());
13    if (this.multipartConfig != null) {
14       registration.setMultipartConfig(this.multipartConfig);
15    }
16    return registration;
17 }

四、总结

这次我们学些了怎样注册传统的三大组件,虽然用到的机会可能不会那么多了,但是springboot的设计理念还是很好的。

原文地址:https://www.cnblogs.com/lookings/p/11719568.html

时间: 2024-08-30 12:26:27

SpringBoot起飞系列-配置嵌入式Servlet容器(八)的相关文章

SpringBoot源码学习系列之嵌入式Servlet容器

1.前言简单介绍 SpringBoot的自动配置就是SpringBoot的精髓所在:对于SpringBoot项目是不需要配置Tomcat.jetty等等Servlet容器,直接启动application类既可,SpringBoot为什么能做到这么简捷?原因就是使用了内嵌的Servlet容器,默认是使用Tomcat的,具体原因是什么?为什么启动application就可以启动内嵌的Tomcat或者其它Servlet容器?ok,本文就已SpringBoot嵌入式Servlet的启动原理简单介绍一下

配置嵌入式Servlet容器

SpringBoot默认是用的是Tomcat作为嵌入式的Servlet容器:问题?1).如何定制和修改Servlet容器的相关配置:1.修改和server有关的配置(ServerProperties): server.port=8081 server.context-path=/crud server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器设置 server.xxx //Tomcat的设置 server.tomcat.xxx 2.编写一个Embedded

springboot(八) 嵌入式Servlet容器自动配置原理和容器启动原理

1.嵌入式Servlet容器自动配置原理 1.1 在spring-boot-autoconfigure-1.5.9.RELEASE.jar => springboot自动配置依赖 jar包下,EmbeddedServletContainerAutoConfiguration => 嵌入式servlet容器自动配置类 @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Configuration @ConditionalOnWebApplicatio

SpringBoot(七) -- 嵌入式Servlet容器

一.嵌入式Servlet容器 在传统的开发中,我们在完成开发后需要将项目打成war包,在外部配置好TomCat容器,而这个TomCat就是Servlet容器.在使用SpringBoot开发时,我们无需再外部配置Servlet容器,使用的是嵌入式的Servlet容器(TomCat).如果我们使用嵌入式的Servlet容器,存在以下问题: 1.如果我们是在外部安装了TomCat,如果我们想要进行自定义的配置优化,可以在其conf文件夹下修改配置文件来实现.在使用内置Servlet容器时,我们可以使用

SpringBoot-配置嵌入式Servlet容器(十三)

SpringBoot默认使用Tomcat作为嵌入式的Servlet容器: 如何定制和修改Servlet容器的相关配置 1.修改和server有关的配置(ServerProperties[也是EmbeddedServletContainerCustomizer]): server.port=8081 server.context-path=/crud server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器设置 server.xxx //Tomcat的设置 s

SpringBoot配置嵌入式的Servlet

SpringBoot默认使用Tomcat作为嵌入式的Servlet 问题? 1).如何定制和修改Servlet容器的相关配置: server.port=8081 server.context-path=/crud server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器设置 server.xxx //Tomcat的设置 server.tomcat.xxx 注:在2.0之前的版本当中可以使用以下方式修改 Servlet  (EmbeddedServletCon

SpringBoot的Web配置

重写全局配置 如果springboot提供的springmvc配置不符合要求,则可以通过一个配置类(标有@Configuration注解的类)加上@EnableWebMvc注解来实现完全自己控制的mvc配置 当你既想保留springboot提供的配置,又想增加自己额外的配置时,可以定义一个配置类并继承WebMvcConfigurationAdapter,无须使用@EnableWebMvc注解 servlet容器配置(servlet容器配置) 如果需要使用代码的方式配置servlet容器,则可以注

使用外置的Servlet容器

嵌入式Servlet容器: 优点:简单.便捷 缺点:默认不支持JSP.优化定制比较复杂(使用定制器[ServerProperties.自定义EmbeddedServletContainerCustomizer]),自己编写嵌入式Servlet容器的创建工厂[EmbeddedServletContainerFactory]: 外置的Servlet容器:外面安装Tomcat--应用war包的方式打包 步骤: 1).必须创建一个war项目(利用idea创建好目录结构) 2).将嵌入式的Tomcat指定

SpringBoot嵌入式Servlet配置原理

SpringBoot嵌入式Servlet配置原理 SpringBoot修改服务器配置 配置文件方式方式修改,实际修改的是ServerProperties文件中的值 server.servlet.context-path=/crud server.port=8081 Java代码方式修改.通过实现WebServerFactoryCusomizer接口来获取到达ConfigurableServletWebServerFactory的通道,ConfigurableServletWebServerFac