SpringBoot2.0以上版本如何定制和修改Servlet容器的相关配置 -- 源码分析(一)

SpringBoot2.0中,配置嵌入式Servlet容器,EmbeddedServletContainerCustomizer 已经被 

WebServerFactoryCustomizer代替,

1.第一种方式:修改配置文件

  • 修改和server有关的配置 (ServerProperties)

    server.port=8081
    server.context-path=/crud
    
    server.tomcat.uri-encoding=UTF-8
    
    //通用的Servlet容器设置
    server.xxx
    //Tomcat的设置
    server.tomcat.xxx

2.第二种方式:编写一个WebServerFactoryCustomizer

:嵌入式的Servlet容器的定制器;来修改Servlet容器的配置

查看源码:

(1)WebServerFactoryCustomizer是一个接口

@FunctionalInterface
public interface WebServerFactoryCustomizer<T extends WebServerFactory> {

	/**
	 * Customize the specified {@link WebServerFactory}.
	 * @param factory the web server factory to customize
	 */
	void customize(T factory);

}
public interface WebServerFactory {

}

(2)Ctrl + Alt + B 查看 WebServerFactory的实现类:

(3)我们发现:

可以实现ConfigurableWebServerFactory 接口,并重写其中的方法;可以达到我们想要的效果
public interface ConfigurableWebServerFactory extends WebServerFactory, ErrorPageRegistry {

	/**
	 * Sets the port that the web server should listen on. If not specified port '8080'
	 * will be used. Use port -1 to disable auto-start (i.e start the web application
	 * context but not have it listen to any port).
	 * @param port the port to set
	 */
	void setPort(int port);

	/**
	 * Sets the specific network address that the server should bind to.
	 * @param address the address to set (defaults to {@code null})
	 */
	void setAddress(InetAddress address);

	/**
	 * Sets the error pages that will be used when handling exceptions.
	 * @param errorPages the error pages
	 */
	void setErrorPages(Set<? extends ErrorPage> errorPages);

(4)在我们自定义的MyMvcConfig类,定义方法:

@Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            //定制嵌入式的servlet容器相关的规则
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8083);
            }
        };
    }

因此,针对如何修改SpringBoot的默认配置,我们有几种思想:

模式:

1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;

2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置

3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot 2.0及以上版本中,可以通过以下步骤配置两个端口: 1. 在`application.properties`或`application.yml`配置文件中添加以下属性: ```yaml server.port=8080 # 添加第二个端口配置 server.additional-ports=9090 ``` 2. 创建一个自定义的`WebServerFactoryCustomizer` bean,并在其中进行端口配置。可以在应用的主类上添加`@Configuration`注解,并定义一个bean方法来创建自定义的`WebServerFactoryCustomizer`。 ```java import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ServerConfig { @Bean public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() { return (WebServerFactoryCustomizer<ConfigurableWebServerFactory>) factory -> { if (factory instanceof ConfigurableServletWebServerFactory) { // 配置第二个端口 ((ConfigurableServletWebServerFactory) factory).setPort(9090); } }; } } ``` 这里使用了`ConfigurableServletWebServerFactory`来进行配置。通过创建一个自定义的`WebServerFactoryCustomizer` bean,并实现`WebServerFactoryCustomizer<ConfigurableWebServerFactory>`接口,在`customize()`方法中对第二个端口进行配置。 这样配置完成后,你的Spring Boot应用就会同时监听8080和9090两个端口。你可以根据需求进行相应的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值