欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot框架配置文件路徑設(shè)置方式

 更新時(shí)間:2021年11月19日 10:31:54   作者:Jone_ZhangH  
這篇文章主要介紹了SpringBoot框架配置文件路徑設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot配置文件路徑設(shè)置

選擇“Edit Configurations”:

在這里插入圖片描述

下springboot啟動(dòng)配置中修改VM options的值:

在這里插入圖片描述

值參考:

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

自定義配置文件路徑以及多profile配置文件

一、什么是classpath

classpath 指的就是 *.java 文件、資源文件等編譯后存放的位置,對于 Maven 項(xiàng)目就是指 target/classes這個(gè)路徑,只要編譯后的文件在這個(gè)目錄下,springboot 就可以找到,我們這里指的是 Maven 項(xiàng)目, Java Web 項(xiàng)目可能會(huì)有區(qū)別.

 

二、自定義springboot配置文件路徑

springboot 項(xiàng)目配置文件 application.properties/application.yml 默認(rèn)放置的位置是 classpath:/、classpath:/config/、file:./、file:./config/ 這4個(gè)位置.只要我們編譯后的文件位于這 4 個(gè)位置,springboot 就可以加載配置文件.但有時(shí)候我們需要以環(huán)境名稱為標(biāo)識(shí),配置多個(gè)環(huán)境的配置文件.如下我們需要將配置文件放置在指定的環(huán)境標(biāo)識(shí)下.

如上圖我們的資源文件的路徑已經(jīng)不再是 springboot 默認(rèn)的配置路徑了,因此 springboot 啟動(dòng)時(shí)將無法加載配置文件,需要我們在啟動(dòng)類中手動(dòng)指定配置文件的加載路徑了.

@SpringBootApplication
public class Springboot01Application {
    public static void main(String[] args) {
        // springboot 默認(rèn)的配置文件路徑
        // 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 配置文件的切換

我們平時(shí)開發(fā)項(xiàng)目的時(shí)候,在不同的時(shí)期會(huì)存在不同的環(huán)境配置文件,例如: application-dev.properties、application-sit.properties、application-uat.properties、application-pro.properties,那么我們運(yùn)行項(xiàng)目的時(shí)候如何做到根據(jù)當(dāng)前所處的環(huán)境來切換對應(yīng)的環(huán)境配置文件呢?

springboot 為我們提供了切換配置文件的方式,我們在編寫配置文件時(shí),文件名可以是: application-{profile}.properties 或者是 application-{profile}.yml

1、application-{profile}.properties方式

一、各環(huán)境配置文件如下:

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

二、實(shí)體類

@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!!!";
    }
}

四、測試結(jié)果

2、application.yaml方式

spring:
   profiles:
      active: uat # 指定激活哪個(gè)配置文件
---
 
server:
   port: 8082
spring:
   profiles: dev 
---
 
server:
   port: 8083
spring:
   profiles: sit  
---
 
server:
   port: 8084
spring:
   profiles: uat

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論