Spring Boot 自定义端口等

一、Spring Boot 1.X

/**
 * Created by jiankunking on 2018/2/26.
 */
@RestController
@EnableAutoConfiguration
public class CustomPortController implements EmbeddedServletContainerCustomizer {
    /**
     * 自定义端口
     *
     * @param container
     */
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(EnvionmentVariables.PORT);
    }

    @RequestMapping("/")
    public String setPort(int port) {
        return String.valueOf(port);
    }

}

二、Spring Boot 2.X

package com.jiankunking.elasticsearch.extension.customizer;

import com.jiankunking.elasticsearch.extension.config.EnvionmentVariables;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jiankunking.
 * @date:2018/8/17 10:05
 * @description:
 */
@RestController
@EnableAutoConfiguration
public class CustomPortController implements WebServerFactoryCustomizer {
    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        //设置自定义监听端口,我这里是从环境变量取的
        factory.setPort(EnvionmentVariables.LISTEN_PORT);
    }
}

个人微信公众号:
这里写图片描述

作者:jiankunking 出处:http://blog.csdn.net/jiankunking

你可能感兴趣的:(Spring)