admin管理员组

文章数量:1534198

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) {
        // 启动springboot框架
        SpringApplication.run(Application.class,args);
    }
}

/*
    Springboot内置了tomcat,不用再配置tomcat。但是需要创建一个启动类,作为入口。
    启动项目的时候,运行该main就可以

    1. 类名叫Application
    2. 类必须建在com.baizhi。只有建在这个包里面,springboot才会扫描本包以及子包中的注解。
    3. 在这类上面添加注解。@SpringBootApplication

*/
pom.xml(springboot项目初始化,不包括后续)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache/POM/4.0.0"
         xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache/POM/4.0.0 http://maven.apache/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>

    <!--配置jdk的版本,因为springboot2依赖了spring5,spring5又依赖了jdk1.8,所以我们的项目至少是jdk1.8.-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mavenpiler.source>1.8</mavenpiler.source>
        <mavenpiler.target>1.8</mavenpiler.target>
    </properties>

    <dependencies>

        <!--引入依赖。引入一个就可以了。这一个就相当于spring的核心jar包和springmvc的相关jar包。-->
        <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>

本文标签: 打开浏览器项目SpringBoot