JavaWeb——SpringBoot 系列(5)SpringBoot内嵌 Tomcat的配置
其他
2020-02-08 18:23:45
阅读次数: 0
- 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 容器配置
2、Tomcat 特有配置
二、代码方式配置 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 {
@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";
}
public static void main(String[] 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
2、Undertow
上一篇
下一篇
发布了146 篇原创文章 ·
获赞 15 ·
访问量 1万+
转载自blog.csdn.net/qq_42896653/article/details/104169719