SpringBoot配置文件路径 SpringBoot框架配置文件路径设置方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot配置文件路径 SpringBoot框架配置文件路径设置方式

Jone_ZhangH   2021-11-19 我要评论
想了解SpringBoot框架配置文件路径设置方式的相关内容吗,Jone_ZhangH在本文为您仔细讲解SpringBoot配置文件路径的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot框架,配置文件路径,文件路径设置,下面大家一起来学习吧。

SpringBoot配置文件路径设置

选择“Edit Configurations”:

在这里插入图片描述

下springboot启动配置中修改VM options的值:

在这里插入图片描述

值参考:

-Dspring.config.location=E:/workspace/xxxx/application.yml
-Dspring.config.location=E:/workspace/xxxx/db.yml

自定义配置文件路径以及多profile配置文件

一、什么是classpath

classpath 指的就是 *.java 文件、资源文件等编译后存放的位置,对于 Maven 项目就是指 target/classes这个路径,只要编译后的文件在这个目录下,springboot 就可以找到,我们这里指的是 Maven 项目, Java Web 项目可能会有区别.

 

二、自定义springboot配置文件路径

springboot 项目配置文件 application.properties/application.yml 默认放置的位置是 classpath:/、classpath:/config/、file:./、file:./config/ 这4个位置.只要我们编译后的文件位于这 4 个位置,springboot 就可以加载配置文件.但有时候我们需要以环境名称为标识,配置多个环境的配置文件.如下我们需要将配置文件放置在指定的环境标识下.

如上图我们的资源文件的路径已经不再是 springboot 默认的配置路径了,因此 springboot 启动时将无法加载配置文件,需要我们在启动类中手动指定配置文件的加载路径了.

@SpringBootApplication
public class Springboot01Application {
    public static void main(String[] args) {
        // springboot 默认的配置文件路径
        // String addClassPath = "spring.config.location:classpath:/";
        // 自定义的配置文件路径
        String addClassPath = "spring.config.additional-location:classpath:/";
        addClassPath += ",classpath:/config/env/";
        new SpringApplicationBuilder(Springboot01Application.class).
                properties("spring.config.name:application", addClassPath).build().run(args);
    }
}

三、多 profiles 配置文件的切换

我们平时开发项目的时候,在不同的时期会存在不同的环境配置文件,例如: application-dev.properties、application-sit.properties、application-uat.properties、application-pro.properties,那么我们运行项目的时候如何做到根据当前所处的环境来切换对应的环境配置文件呢?

springboot 为我们提供了切换配置文件的方式,我们在编写配置文件时,文件名可以是: application-{profile}.properties 或者是 application-{profile}.yml

1、application-{profile}.properties方式

一、各环境配置文件如下:

application-dev.properties

person.name=xiaomaomao-dev
person.pet=dog-dev
person.hobby=LOL-dev
person.cleverLevel=nine-dev

application-sit.properties

person.name=xiaomaomao-sit
person.pet=dog-sit
person.hobby=LOL-sit
person.cleverLevel=nine-sit

application-uat.properties

person.name=xiaomaomao-uat
person.pet=dog-uat
person.hobby=LOL-uat
person.cleverLevel=nine-uat

application-pro.properties 

person.name=xiaomaomao-pro
person.pet=dog-pro
person.hobby=LOL-pro
person.cleverLevel=nine-pro

通过 application.properties 来选择需要激活的配置文件

spring.profiles.active=sit

二、实体类

@Configuration
@PropertySource("classpath:config/env/application.properties")
@ConfigurationProperties(prefix = "person")
// 省略了 get/set
public class Person {
    private String name;
    private String pet;
    private String hobby;
    private String cleverLevel;
}

三、控制器

@RestController
public class SpringbootProfileController {
    @Autowired
    private ApplicationContext ioc;
    @RequestMapping(value = "/profile")
    public String testProfiles() {
        Person person = ioc.getBean("person", Person.class);
        System.out.println(person);
        return "hello profiles!!!";
    }
}

四、测试结果

2、application.yaml方式

spring:
   profiles:
      active: uat # 指定激活哪个配置文件
---
 
server:
   port: 8082
spring:
   profiles: dev 
---
 
server:
   port: 8083
spring:
   profiles: sit  
---
 
server:
   port: 8084
spring:
   profiles: uat

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们