SpringBoot的Web开发入门案例4—修改和容器相关的配置
以修改tomcat端口为例,默认端口为8080
在application.yml中配置端口为8088:
server:
port: 8088
通过编程的方式配置端口–方式1
创建 MyServerConfig 类,该类继承WebServerFactoryCustomizer,泛型为ConfigurableServletWebServerFactory。实现customize方法,通过参数factory修改配置。
该类需加入容器
package com.blu.conf;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
@Component
public class MyServerConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>{
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(9000);
}
}
重启项目,端口将变为9000
通过编程的方式配置端口–方式2
在配置类中添加配置Bean:
@Bean
public ConfigurableServletWebServerFactory tomcatServerFactory() {
TomcatServletWebServerFactory tomcatconfig = new TomcatServletWebServerFactory();
tomcatconfig.setPort(60001);
return tomcatconfig;
}
重启项目,端口将变为60001