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

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

application.properties配置文件

fileName=configName

PropertiesConfig類文件

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

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

  public String getFileName() {
    return fileName;
  }

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

測試

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());//結果輸出:configName
    assert "configName".equals(propertiesConfig.getFileName());
  }
}

二、@ConfigurationProperties讀取多個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;
  }
}

測試

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());//結果輸出: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;
  }
}

測試

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());//結果輸出:configName
    assert "root".equals(propertySourceConfig.getUserName());
    System.out.println(propertySourceConfig.getUserName());//結果輸出:root
    assert "root".equals(propertySourceConfig.getPassWord());
    System.out.println(propertySourceConfig.getPassWord());//結果輸出:root
  }
}

完整代碼鏈接:read-config-file項目地址

到此這篇關於詳解SpringBoot註解讀取配置文件的方式的文章就介紹到這瞭,更多相關SpringBoot註解讀取配置文件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: