SpringBoot项目自动打开浏览器配置

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.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>

    <!--配置jdk的版本,因为springboot2依赖了spring5,spring5又依赖了jdk1.8,所以我们的项目至少是jdk1.8.-->
    <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>

        <!--引入依赖。引入一个就可以了。这一个就相当于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>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值