一、
在application.properties文件中配置
#配置tomcat端口,针对内置的tomcat #server.port=8888 #绑定ip地址,只能由设置的地址进入访问 #server.address=172.0.0.1 #配置context-path,访问时,需要在连接后加上配置的路径 localhost:8888/server #server.servlet.context-path=/server
二、实现 WebServerFactoryCustomizer 接口
1、
@Component
public class TomcatCfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setContextPath("/demo");
factory.setPort(8090);
}
}
2、
WebServerFactoryCustomizer里面绑定 TomcatServletWebServerFactory 可以设置tomcat BIO / NIO(Tomcat7之后采用的默认NIO,)
设置连接器 Connector
@Component
public class TomcatCfig implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.setContextPath("/demo");
factory.setPort(8090);
}
}
同时springboot支持jetty
pom文件要加入下面注入来去除springboot 中默认的Tomcat 依赖
<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>
@Component//启动jetty
public class Jetty implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {
@Override
public void customize(JettyServletWebServerFactory factory) {
System.out.println("jetty================");
factory.setContextPath("/demo");
factory.setPort(8090);
}
}
