• SpringBoot04-servlet


    配置嵌入式Servlet容器

    1. Servlet默认使用Tomcat作为嵌入式的Servlet容器
    2. 如何定制和修改Servlet容器的相关配置?
      1. 修改和Server有关的配置(ServerProperties, 其实也是一个WebServerFactoryCustomizer) 
        server.port=8081
        server.context-path=/xxx
        
        server.tomcat.uri-encoding=UTF-8
        
        //通用的Servlet容器设置
        server.xxx
        
        //Tomcat的设置
        server.tomcat.xxx
      2. 编写一个WebServerFactoryCustomizer<ConfigurableWebServerFactory>: 嵌入式的Servlet容器的定制器, 来修改Servlet容器的配置, (比起1, 优先级更高)
            @Bean
            public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
                return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
        //定制嵌入式的Servlet容器相关的规则 @Override
        public void customize(ConfigurableWebServerFactory factory) { factory.setPort(8081); } }; }
    3. 注册Servlet, Filter, Listener三大组件.
      1. 由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用, 没有web.xml文件, 所以注册三大组件用以下方式.
      2. ServletRegistrationBean
        @Configuration
        public class MyServerConfig {
        
            //注册三大组件
            @Bean
            public ServletRegistrationBean myServlet() {
        
                ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(), "/myServlet");
                servletRegistrationBean.setLoadOnStartup(1);
                return servletRegistrationBean;
            }
        }
      3. FilterRegistrationBean
            @Bean
            public FilterRegistrationBean myFilter() {
                FilterRegistrationBean registrationBean = new FilterRegistrationBean();
                registrationBean.setFilter(new MyFilter());
                registrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
                return registrationBean;
            }
      4. ServletListenerRegistrationBean
            @Bean
            public ServletListenerRegistrationBean myListener() {
                ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
                return registrationBean;
            }
      5. 举个例子: SpringBoot再帮我们自动配置SpringMVC时, 自动注册前端控制器DispatcherServlet, 其中DispatcherServletAutoConfiguration
        @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
        @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
        public ServletRegistrationBean dispatcherServletRegistration(
              DispatcherServlet dispatcherServlet) {
           ServletRegistrationBean registration = new ServletRegistrationBean(
                 dispatcherServlet, this.serverProperties.getServletMapping());
        //默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求; /*会拦截jsp //可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径 registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); registration.setLoadOnStartup( this.webMvcProperties.getServlet().getLoadOnStartup()); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } return registration; }
    4. 使用其他的Servlet容器
      1. Tomcat(默认使用), 先排除掉
                <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>
      2. Undertow(不支持Jsp, 但并发处理能力很好).
        <dependency>
           <artifactId>spring-boot-starter-undertow</artifactId>
           <groupId>org.springframework.boot</groupId>
        </dependency>
      3. Jetty(长连接)
        <dependency>
           <artifactId>spring-boot-starter-jetty</artifactId>
           <groupId>org.springframework.boot</groupId>
        </dependency>

    使用外置的Servlet容器

    1. 嵌入式Servlet容器: 打jar包
      1. 优点: 简单便捷.
      2. 缺点: 默认不支持Jsp, 优化定制Servlet容器很复杂(定制器, 自己编写嵌入式Servlet容器的创建工厂).
    2. 外置的Servlet容器: 外面安装Tomcat, 应用war包的方式打包.
    3. 步骤
      1. 必须创建一个war项目: 利用idea创建好目录结构
      2. 将嵌入式的Tomcat指定为provided
        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
           <scope>provided</scope>
        </dependency>
      3. 必须编写一个SpringBootServletInitializer, 并且调用configure方法
        public class ServletInitializer extends SpringBootServletInitializer {
        
            @Override
            protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                return application.sources(SpringBoot04WebWarApplication.class);
            }
        }
      4. 启动服务器
  • 相关阅读:
    UI组件库Kendo UI for Vue原生组件中文教程 如何开始制作动画
    高性能HTML5/JavaScript开发框架DevExtreme v22.1.3正式发布
    界面组件Telerik UI for WPF入门指南 如何使用主题切换自定义样式
    如何在MVVM场景中使用WinUI数据网格?这个工具可以搞定
    Spring Boot设置上传文件大小
    SpringBoot SpringBoot 定时器的三种方式定时器的三种方式
    SpringBoot文件上传
    vscode快速生成vue模板
    Failed to execute goal org.apache.maven.plugins:mavensurefireplugin:2.12.4
    There are no resources that can be added or removed from the server
  • 原文地址:https://www.cnblogs.com/binwenhome/p/12895864.html
Copyright © 2020-2023  润新知