springboot项目自启动浏览器网页

新建类,自己指定打开页面

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
 * spring boot 容器加载后自动监听
 */
@Component
public class BootStartConfig implements CommandLineRunner {
    
    

    @Override
    public void run(String... args) {
    
    
        try {
    
    
            //指定自己的网址路径,使用cmd命令行打开
            Runtime.getRuntime().exec("cmd   /c   start   http://localhost:8080/house");
        } catch (Exception ex) {
    
    
            ex.printStackTrace();
        }
    }

}

新建类,自动获取端口文件配置打开

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
 * spring boot 容器加载后自动监听
 */
@Component
public class BootStartConfig implements CommandLineRunner {
    
    

    //获取配置文件端口和路径名
    @Value("${server.port}")
    private String port;
    @Value("${server.servlet.context-path}")
    private String path;

    @Override
    public void run(String... args) {
    
    
        try {
    
    
        	//指定自己的网址路径,使用cmd命令行打开
            Runtime.getRuntime().exec("cmd   /c   start   http://localhost:"+port+path);
        } catch (Exception ex) {
    
    
            ex.printStackTrace();
        }
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_48860638/article/details/121135919