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

詳解SpringBoot注解讀取配置文件的方式

 更新時(shí)間:2021年02月10日 09:35:06   作者:傾如故  
這篇文章主要介紹了詳解SpringBoot注解讀取配置文件的方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、@Value讀取application.properties配置文件中的值

application.properties配置文件

fileName=configName

PropertiesConfig類文件

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class PropertiesConfig {
  //通過(guò)@Value注解讀取fileName的值
  @Value("${fileName}")
  private String fileName;

  public String getFileName() {
    return fileName;
  }

  public void setFileName(String fileName) {
    this.fileName = fileName;
  }
}

測(cè)試

import com.model.PropertiesConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertiesConfigTest {
  @Autowired
  private PropertiesConfig propertiesConfig;
  @Test
  public void test(){
    System.out.println(propertiesConfig.getFileName());//結(jié)果輸出:configName
    assert "configName".equals(propertiesConfig.getFileName());
  }
}

二、@ConfigurationProperties讀取多個(gè)application.properties配置文件中的值

application.properties文件

database.username=root
database.password=root

DatabaseConfig類文件

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("database")
public class DatabaseConfig {
  private String userName;
  private String passWord;

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassWord() {
    return passWord;
  }

  public void setPassWord(String passWord) {
    this.passWord = passWord;
  }
}

測(cè)試

import com.model.DatabaseConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DatabaseConfigTest {
  @Autowired
  private DatabaseConfig databaseConfig;
  @Test
  public void test(){
    System.out.println("username = " + databaseConfig.getUserName() +", password = "+databaseConfig.getPassWord());//結(jié)果輸出:username = root, password = root
    assert "root".equals(databaseConfig.getUserName());
    assert "root".equals(databaseConfig.getPassWord());
  }
}

三、@PropertySource讀取任意配置文件

新建property-source.properties配置文件

fileName=configName
database.username=root
database.password=root

PropertySourceConfig類文件

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource(value = {"classpath:property-source.properties"})
@ConfigurationProperties("database")
public class PropertySourceConfig {
  @Value("${fileName}")
  private String fileName;
  private String userName;
  private String passWord;

  public String getFileName() {
    return fileName;
  }

  public void setFileName(String fileName) {
    this.fileName = fileName;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassWord() {
    return passWord;
  }

  public void setPassWord(String passWord) {
    this.passWord = passWord;
  }
}

測(cè)試

import com.model.PropertySourceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertySourceConfigTest {

  @Autowired
  private PropertySourceConfig propertySourceConfig;

  @Test
  public void test(){
    assert "configName".equals(propertySourceConfig.getFileName());
    System.out.println("fileName = " + propertySourceConfig.getFileName());//結(jié)果輸出:configName
    assert "root".equals(propertySourceConfig.getUserName());
    System.out.println(propertySourceConfig.getUserName());//結(jié)果輸出:root
    assert "root".equals(propertySourceConfig.getPassWord());
    System.out.println(propertySourceConfig.getPassWord());//結(jié)果輸出:root
  }
}

完整代碼鏈接:read-config-file項(xiàng)目地址

到此這篇關(guān)于詳解SpringBoot注解讀取配置文件的方式的文章就介紹到這了,更多相關(guān)SpringBoot注解讀取配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring創(chuàng)建bean的幾種方式及使用場(chǎng)景

    Spring創(chuàng)建bean的幾種方式及使用場(chǎng)景

    本文主要介紹了Spring創(chuàng)建bean的幾種方式及使用場(chǎng)景,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java高性能緩存框架之Caffeine詳解

    Java高性能緩存框架之Caffeine詳解

    這篇文章主要介紹了Java高性能緩存框架之Caffeine詳解,Caffeine是一個(gè)基于Java8的高性能緩存框架,號(hào)稱趨于完美,Caffeine受啟發(fā)于Guava?Cache的API,使用API和Guava是一致的,需要的朋友可以參考下
    2023-12-12
  • java 自定義可繼承枚舉Enum的案例

    java 自定義可繼承枚舉Enum的案例

    這篇文章主要介紹了java 自定義可繼承枚舉Enum的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • java解決動(dòng)態(tài)配置字段需求問(wèn)題

    java解決動(dòng)態(tài)配置字段需求問(wèn)題

    這篇文章主要介紹了java解決動(dòng)態(tài)配置字段需求問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • springboot使用Thymeleaf報(bào)錯(cuò)常見(jiàn)的幾種解決方案

    springboot使用Thymeleaf報(bào)錯(cuò)常見(jiàn)的幾種解決方案

    這篇文章主要介紹了springboot使用Thymeleaf報(bào)錯(cuò)常見(jiàn)的幾種解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Spring?Boot中自動(dòng)執(zhí)行sql腳本的方法實(shí)例

    Spring?Boot中自動(dòng)執(zhí)行sql腳本的方法實(shí)例

    在SpringBoot的架構(gòu)中,DataSourceInitializer類可以在項(xiàng)目啟動(dòng)后初始化數(shù)據(jù),我們可以通過(guò)自動(dòng)執(zhí)行自定義sql腳本初始化數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中自動(dòng)執(zhí)行sql腳本的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • java實(shí)現(xiàn)Api接口加密通信方式

    java實(shí)現(xiàn)Api接口加密通信方式

    這篇文章主要介紹了java實(shí)現(xiàn)Api接口加密通信方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • MybatisPlus多條件?or()的使用問(wèn)題小結(jié)

    MybatisPlus多條件?or()的使用問(wèn)題小結(jié)

    這篇文章主要介紹了MybatisPlus多條件?or()的使用問(wèn)題小結(jié),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • java獲取http請(qǐng)求的Header和Body的簡(jiǎn)單方法

    java獲取http請(qǐng)求的Header和Body的簡(jiǎn)單方法

    下面小編就為大家?guī)?lái)一篇java獲取http請(qǐng)求的Header和Body的簡(jiǎn)單方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • 淺談Timer和TimerTask與線程的關(guān)系

    淺談Timer和TimerTask與線程的關(guān)系

    下面小編就為大家?guī)?lái)一篇淺談Timer和TimerTask與線程的關(guān)系。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03

最新評(píng)論