纵有疾风起
人生不言弃

springBoot定制内嵌的Tomcat

此篇仅介绍配置方式,详细配置内容百度查阅

工程结构:

springBoot定制内嵌的Tomcat插图

 

可以通过配置的方式设置参数,如下

application.properties

#配置tomcat端口# server.port= 8888#绑定ip地址# server.address=192.168.3.102# 配置contextPath# server.servlet.context-path=/server

 

tomcat使用代码配置

package com.example.boottomcat.config;import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;import org.springframework.boot.web.server.WebServerFactoryCustomizer;@Componentpublic class TomcatConfiguration implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {    @Override    public void customize(TomcatServletWebServerFactory factory) {        System.out.println("--------------------");        factory.setContextPath("/demo");        factory.setPort(8090);        factory.setProtocol("org.apache.coyote.http11.Http11NioProtocol");    }}

 

使用Jetty容器

要引入Jetty依赖,且排除掉自带Tomcat

pom.xml如下

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.example</groupId>    <artifactId>boot-tomcat</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>boot-tomcat</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.0.6.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <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>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

使用代码配置Jetty

package com.example.boottomcat.config;import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;import org.springframework.boot.web.server.WebServerFactoryCustomizer;import org.springframework.stereotype.Component;@Component// 启用jettypublic class JettyConfiguration implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {    @Override    public void customize(JettyServletWebServerFactory factory) {        System.out.println("--------------------");        factory.setContextPath("/demo");        factory.setPort(8090);    }}

 

文章转载于:https://www.cnblogs.com/lm970585581/p/9838670.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » springBoot定制内嵌的Tomcat

分享到: 生成海报
avatar

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活