SpringBoot项目自动打开浏览器配置
resources:application.properties
#tomcat的端口号
server.port=8989
#项目的项目名,以后我们访问该项目http://ip:port/context-path
server.servlet.context-path=/springboot_baizhi
# 启动项目自动打开浏览器
openProject.isOpen=true
openProject.cmd=cmd /c start
openProject.web.openUrl=http://localhost:${server.port}${server.servlet.context-path}/
# 自动打开 http://localhost:8989/springboot_baizhi/
RunConfig.java
package com.qhx.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RunConfig implements CommandLineRunner {
@Value("${openProject.isOpen}")
private boolean isOpen;
@Value("${openProject.web.openUrl}")
private String openUrl;
@Value("${openProject.cmd}")
private String cmd;
@Override
public void run(String... args){
if(isOpen){
String runCmd = cmd + " " + openUrl ;
System.out.println("运行的命令: " + runCmd);
Runtime run = Runtime.getRuntime();
try {
run.exec(runCmd);
System.out.println("启动浏览器打开项目成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("启动项目自动打开浏览器失败");
}
}
}
}
其他配置
com.xxx.application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
pom.xml(springboot项目初始化,不包括后续)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringBoot_baizhi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>
>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>