spring boot資源文件配置讀取


一般業務配置,盡量新建自己的配置文件,來讀取,而不是配置在application.properties或application-*.properties或yml/yaml配置中。

application.properties或application-*.properties中如果配置了中文內容,必須得轉成Unicode碼,否則讀取亂碼。

轉Unicode碼可以使用jdk自帶工具,cmd切換到jdk下的bin目錄,打開native2ascii.exe,然后在下面輸入你的中文,回車顯示Unicode碼,復制到配置文件中來。

整個資源文件中文切換Unicode:cmd切換到jdk下的bin目錄,輸入native2ascii -encoding utf-8 源文件名.properties 目標文件名.properties

springboot默認properies配置文件內部源代碼使用ISO-8859-1編碼,即使更改這文件屬性編碼也是無效的,詳細可以參考這篇博文:https://blog.csdn.net/formemorywithyou/article/details/96473169

但yml/yaml中卻可以配置中文,正常讀取,具體源碼也可參考上述地址。

而自定義配置文件,可以指定編碼獲取,所以,建議業務配置新建自己的配置文件存儲,讀取。

另外,如果同時存在application yml和properties配置文件,properties中的配置會覆蓋yml中相同配置。

 

分三種方式,讀取資源配置

1,通過@value("${key}")獲取配置值

     在application.properties中,配置:demo.title=\u6d4b\u8bd5\u6d4b\u8bd5

     這里因為是springboot默認配置,所以中文要轉Unicode碼,不然亂碼,見上面文字說明。

     然后,在test類中的對應屬性上通過@value("${demo.title}")加載

    注意,如果配置在自定義文件中,還需要指定資源文件位置,這里是直接配置在application.properties默認配置中的。

@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
    @Value("${demo.title}")
    private String title;
    @Test
    public void testProperties()throws Exception{
        System.out.println(title);
    }
}

2,通過key的前綴,統一獲取配置值

   繼續讀取【在application.properties中,配置:demo.title=\u6d4b\u8bd5\u6d4b\u8bd5】

   新建個TestPropertis類,類頭部通過注解@ConfigurationProperties(prefix="demo")設置properties key的前綴

   類內部通過屬性key后綴來獲取值

  

@Component
@ConfigurationProperties(prefix="demo") public class TestPropertis { private String title; //setter getter... }

注意:使用注解@ConfigurationProperties需要引入依賴

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>

在讀取的類中通過@Resource引入TestProperties

@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
  @Resource
    private TestPropertis testPropertis;
    @Test
    public void testProperties2()throws Exception{
        System.out.println("TEST----------"+testPropertis.getTitle());
    }
}

3、自定義配置文件,通過key前綴統一獲取

在src/main/resources新建一個demo.properties,文件屬性確保UTF-8

 

在文件中配置示例:

demo.name=小小灰
demo.sex=M

然后新建一個類:TestDemoPropertis,類頭部加入3個注解

@Component     //生成實例,方便springboot調用

@ConfigurationProperties(prefix="demo")    //統一設置資源文件key的前綴  //類中的每個字段為key的對應后綴  

@PropertySource(value={"classpath:demo.properties"},encoding="utf-8")  

//指定配置資源文件位置,這里是string數組,可以多個

//encoding必須加上,如果要讀取中文,不然亂碼,因為springboot源碼加載properties是以ISO-8859-1讀取的

@Component
@ConfigurationProperties(prefix="demo")
@PropertySource(value={"classpath:demo.properties"},encoding="utf-8")
public class TestDemoPropertis {

    private String name;
    private String sex;
    //setter getter...
}

注意:使用@ConfigurationProperties要引入依賴

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>

最后,同上一種在類中通過Resource引入TestDemoPropertis

@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
    @Resource
    private TestDemoPropertis testDemoPropertis;
    @Test
    public void testProperties3()throws Exception{
        System.out.println("TEST DEMO----------"+testDemoPropertis.getName()+"-----"+testDemoPropertis.getSex());
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM