JavaWeb——SpringBoot 系列(5)SpringBoot内嵌 Tomcat的配置

  • spring Boot 内嵌有 Tomcat 容器。

一、使用 application.properties 配置

  • Spring Boot 在 org.springframework.boot.autoconfigure.web.ServerProperties 文件中定义 Tomcat 的所有属性:
    @ConfigurationProperties(
    prefix = "server",
    ignoreUnknownFields = false
    )
    public class ServerProperties implements EmbeddedServletContainerCustomizer, Ordered {
        private Integer port;
        private InetAddress address;
        private Integer sessionTimeout;
        private String contextPath;
        private String displayName = "application";
        @NestedConfigurationProperty
        private Ssl ssl;
        @NotNull
        private String servletPath = "/";
        private final ServerProperties.Tomcat tomcat = new ServerProperties.Tomcat();
        private final ServerProperties.Undertow undertow = new ServerProperties.Undertow();
        @NestedConfigurationProperty
        private JspServlet jspServlet;
        private final Map<String, String> contextParameters = new HashMap();
        }
    
  • 因此,我们配置 Tomcat 时只需要在 application.properties 中进行属性配置即可。

1、Servlet 容器配置

  • 因为 Spring Boot 默认内嵌的 Tomcat 为 Servlet 容器。
  • 通用的 Servlet 容器配置以 “server” 为前缀。
  • 示例:
    server.port = #配置程序的端口,供外部设备访问,默认为8080
    server.seesion-timeout=#用户会话 session 的过期时间,以秒为单位
    server.context-path=#配置访问路径,默认为/
    

2、Tomcat 特有配置

  • Tomcat 特有的配置,用 “server.tomcat" 作为前缀。
  • 示例:
    server.tomcat.uri-encoding=#配置 Tomcat 编码,默认为 UTF-8
    server.tomcat.compression=#Tomcat 是否开启压缩,默认关闭 off
    
  • 详细的 servlet 和 Tomcat 配置可以查看链接导向的官方文档:servlet 详细配置

二、代码方式配置 Tomcat

1、通用配置

  • 用代码的方式为 Servlet 进行通用配置的操作,就是建立一个实现 EmbeddedServletContaintCustomizer 接口的 Bean。

1.1、新建类的方式

  • 示例如下:
    import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
    import org.springframework.boot.context.embedded.ErrorPage;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Component;
    
    import java.util.concurrent.TimeUnit;
    
    @Component
    public class CustomServletContainer implements EmbeddedServletContainerCustomizer {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            container.setPort(8888);    //设置启动端口
            container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,"/404.html"));   //设置错误页面
            container.setSessionTimeout(10, TimeUnit.MINUTES);  //设置访问超时时间
        }
    }
    

1.2、在已有配置文件中

  • 在项目现有的配置文件中增加 Bean 的方式来配置的话,需要将类声明为 static,示例如下:
    import com.wisely.ch5_2_3.bean.Person;
    import com.wisely.ch5_2_3.config.AuthorSettings;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
    import org.springframework.boot.context.embedded.ErrorPage;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    @Controller
    @SpringBootApplication
    public class Ch523Application {
    
    //    @Value("${author.name}")
    //    private String authorName;
    //    @Value("${author.mail}")
    //    private String authorMail;
    //    @Autowired
    //    private AuthorSettings authorSettings;
    
        @RequestMapping("/")
        public String index(Model model){
            Person single = new Person("aa",11);
            List<Person> people = new ArrayList<>();
            Person p1 = new Person("xx",11);
            Person p2 = new Person("yy",22);
            Person p3 = new Person("zz",33);
            people.add(p1);
            people.add(p2);
            people.add(p3);
            model.addAttribute("singlePerson",single);
            model.addAttribute("people",people);
            return "index";
    //        return "author name is"+authorSettings.getName()+", age is:"+authorSettings.getAge()
    //                +", mail is:"+authorSettings.getMail();
    //        return "Spring Boot Demo Project, "+"author name is:"+authorName
    //                +", author mail is:"+authorMail;
        }
    
        public static void main(String[] args) {
    
    //        SpringApplication.run(Ch523Application.class, args);
    //        SpringApplication app = new SpringApplication(Ch523Application.class);
    //        app.setShowBanner(false);
    //        app.run(args);
            new SpringApplicationBuilder(Ch523Application.class)
                    .showBanner(true)
                    .run(args);
        }
    
        @Component
        public static class CustomServerContainer implements EmbeddedServletContainerCustomizer{
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.setPort(8888);    //设置启动端口
                container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,"/404.html"));   //设置错误页面
                container.setSessionTimeout(10, TimeUnit.MINUTES);  //设置访问超时时间
            }
        }
    }
    
  • 以上两例各自运行后,控制台局部信息:
    在这里插入图片描述
  • 值得注意的是,因为两个文件的类名一样,内容也一样,不要同时注解为 @Component,也就是说要用其中一个就把另一个的注解注释掉。

2、针对容器的特定配置

  • 除了可以通用配置外,Spring Boot 还支持针对某个容器的特定配置,例如针对 Tomcat 容器的,有一个 TomcatEmbeddedServletContainerFactory,使用示例如下:
    @Bean
    public EmbeddedServletContainerFactory servletContainer(){
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setPort(8888);
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,"/404.html"));
        factory.setSessionTimeout(10,TimeUnit.MINUTES);
        return factory;
    }
    

三、替换 Tomcat

  • Spring Boot 支持将默认的容器 Tomcat 进行替换。

1、Jetty

  • 要将容器从 Tomcat 替换为 Jetty,只需要在 pom 文件进行依赖修改即可,将 spring-boot-starter-tomcat 替换为 spring-boot-starter-Jetty,代码如下:
    <!--        将 Tomcat 容器替换为 Jetty-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
    
  • 再次启动项目,可以看到控制台现实的是 Jetty
    在这里插入图片描述

2、Undertow

  • 也可以将容器改为 Undertow,则 POM 文件的相关代码如下:
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    <!--        替换为 Undertow-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
    
  • 运行结果
    在这里插入图片描述

上一篇
下一篇

发布了146 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42896653/article/details/104169719