Springboot项目启动时自动打印访问地址(超简单)

Springboot项目启动时自动打印访问地址

在Springboot项目中每次启动完项目,手动在浏览器输入访问地址太麻烦了。在启动类中加入下方代码,就可高效地在控制台中单击URL访问项目了~

Springboot项目启动时自动打印访问地址(超简单)_第1张图片
Springboot项目启动时自动打印访问地址(超简单)_第2张图片

示例代码:

@SpringBootApplication
@Slf4j
public class WebApplication {
    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(WebApplication.class, args);
        log.info("..######..##.....##..######...######..########..######...######.\n" +
                ".##....##.##.....##.##....##.##....##.##.......##....##.##....##\n" +
                ".##.......##.....##.##.......##.......##.......##.......##......\n" +
                "..######..##.....##.##.......##.......######....######...######.\n" +
                ".......##.##.....##.##.......##.......##.............##.......##\n" +
                ".##....##.##.....##.##....##.##....##.##.......##....##.##....##\n" +
                "..######...#######...######...######..########..######...######.");
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        if (StringUtils.isEmpty(path)) {
            path = "";
        }
        log.info("\n----------------------------------------------------------\n\t" +
                "Application  is running! Access URLs:\n\t" +
                "Local访问网址: \t\thttp://localhost:" + port + path + "\n\t" +
                "External访问网址: \thttp://" + ip + ":" + port + path + "\n\t" +
                "----------------------------------------------------------");
    }

}

关注下方,发送"01"获取更多相关学习教程。

lombok依赖包:

    
        org.projectlombok
        lombok
        1.18.20
    

你可能感兴趣的:(SpringBoot,Java开发,java,spring,boot)