springboot启动项目模板

springBoot启动项目模板

package com.sport.landrunserver;

import com.sport.landrunserver.common.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.util.ObjectUtils;
import tk.mybatis.spring.annotation.MapperScan;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication
@MapperScan(basePackages = "com.sport.landrunserver.mapper")
@EnableScheduling
@EnableAsync
public class LandRunServerApplication {
    private final static Logger logger = LoggerFactory.getLogger(LandRunServerApplication.class);
    public static void main(String[] args) throws UnknownHostException {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(LandRunServerApplication.class).headless(true);
        ConfigurableEnvironment environment = builder.run(args).getEnvironment();
        String activeProfile = SpringContextUtils.getActiveProfile();
        logger.info("(♥◠‿◠)ノ゙ 启动成功,环境:"+ activeProfile + ",端口号:" + environment.getProperty("server.port"));
        if("true".equals(environment.getProperty("swagger.show")) &&
                ("loc".equals(activeProfile) || "dev".equals(activeProfile))){
            // 获取本地ip地址
            if("loc".equals(activeProfile)){
                String hostAddress = InetAddress.getLocalHost().getHostAddress();

                // 本地
                String visitUrl = environment.getProperty("swagger.visitUrl");
                if (!ObjectUtils.isEmpty(visitUrl)) {
                    String url = "IP访问【swagger接口地址】:http://" + hostAddress + ":" + environment.getProperty("server.port")
                            + environment.getProperty("server.servlet.context-path")
                            + visitUrl;
                    logger.info(url);

                    // 设置到粘贴板
                    String clipStrUrl = "http://" + hostAddress + ":" + environment.getProperty("server.port") + environment.getProperty("server.servlet.context-path") + visitUrl;
                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(clipStrUrl), null);
                    logger.info("IP访问接口地址已复制到剪切板,【CTRL+V】即可使用!");
                }
            }
            // 测试环境
            if("dev".equals(activeProfile)){
                // 传输协议
                String transportProtocolType = environment.getProperty("public.network.transportProtocolType");

                // 测试环境ip访问
                String visitUrl = environment.getProperty("swagger.visitUrl");
                String publicNetworkIp = environment.getProperty("public.network.ip");
                if (!ObjectUtils.isEmpty(publicNetworkIp) && !ObjectUtils.isEmpty(visitUrl)) {
                    String url = "IP访问【swagger接口地址】:" + transportProtocolType + "://" + publicNetworkIp + ":" + environment.getProperty("server.port")
                            + environment.getProperty("server.servlet.context-path")
                            + visitUrl;
                    logger.info(url);
                }

                // 测试环境域名访问
                String publicNetworkDoMain = environment.getProperty("public.network.domain");
                if (!ObjectUtils.isEmpty(publicNetworkDoMain) && !ObjectUtils.isEmpty(visitUrl)) {
                    logger.info("域名访问【swagger接口地址】:" + transportProtocolType + "://" + publicNetworkDoMain + environment.getProperty("server.servlet.context-path") + visitUrl);
                }
            }
        }
    }
}


你可能感兴趣的:(java,spring,boot,java,后端)