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

网友投稿 541 2022-11-21

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

目录SpringBoot配置文件路径设置自定义配置文件路径以及多profile配置文件一、什么是classpath二、自定义springboot配置文件路径http://三、多 profiles 配置文件的切换

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=xiaomvAbLYPMaomao-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:

povAbLYPMrt: 8082

spring:

profiles: dev

---

server:

port: 8083

spring:

profiles: sit

---

server:

port: 8084

spring:

profiles: uat

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:获取路径下所有文件以及子文件夹中文件
下一篇:MM32F0010A1T的特点以及闪存的主要特性
相关文章

 发表评论

暂时没有评论,来抢沙发吧~