1. 程式人生 > 程式設計 >SpringBoot讀取配置檔案常用方法解析

SpringBoot讀取配置檔案常用方法解析

首先回憶一下在沒有使用SpringBoot之前也就是傳統的spring專案中是如何讀取配置檔案,通過I/O流讀取指定路徑的配置檔案,然後再去獲取指定的配置資訊。

傳統專案讀取配置方式#

讀取xml配置檔案

  public String readFromXml(String xmlPath,String property) {
    SAXReader reader = new SAXReader();
    Document doc = null;
    try {
      doc = reader.read(new File(xmlPath));
    } catch (DocumentException e) {
      e.printStackTrace();
    }
    Iterator<Element> iterator = doc.getRootElement().elementIterator();
    while (iterator.hasNext()){
      Element element = iterator.next();
      if (element.getQName().getName().equals(property)){
        return element.getTextTrim();
      }
    }
    return null;
  }

讀取.properties配置檔案

 public String readFromProperty(String filePath,String property) {
    Properties prop = new Properties();
    try {
      prop.load(new FileInputStream(filePath));
      String value = prop.getProperty(property);
      if (value != null) {
        return value;
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

SpringBoot讀取配置方式

如何使用SpringBoot讀取配置檔案,從使用Spring慢慢演變,但是本質原理是一樣的,只是SpringBoot簡化配置,通過註解簡化開發,接下來介紹一些常用註解。

@ImportResource註解

這個註解用來匯入Spring的配置檔案,是配置檔案中的內容注入到配置類中,引數是一個數組,可以注入多個配置檔案

程式碼演示:

在SpringBoot專案的resources目錄下建立一個xml配置檔案beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="configBean" class="com.example.test.config.ConfigBean">
    <property name="dbType" value="Oracle"/>
    <property name="driverClassName" value="jdbc.driver.Oracle.OracleDriver"/>
    <property name="host" value="127.0.0.1"/>
    <property name="userName" value="oracle"/>
    <property name="password" value="oracle"/>
  </bean>
</beans>

建立配置類ConfigBean

package com.example.test.config;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * @author Vincente
 * @date 2020/07/12-12:29
 * @desc 配置類
 **/
@Setter
@Getter
@ToString
public class ConfigBean {
  private String dbType;
  private String driverClassName;
  private String host;
  private String userName;
  private String password;
}

新增@ImportResource註解,在SpringBoot專案的啟動類新增

package com.example.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class TestApplication {

  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class,args);
  }
}

測試程式碼

package com.example.test;

import com.example.test.config.ConfigBean;
import org.junit.jupiter.api.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;

@SpringBootTest
@RunWith(SpringRunner.class)

class TestApplicationTests {
  @Autowired
  private ConfigBean configBean;
  @Test
  void testConfigBean(){
    System.out.println(configBean);
  }
}

輸出結果

ConfigBean(dbType=Oracle,driverClassName=jdbc.driver.Oracle.OracleDriver,host=127.0.0.1,userName=oracle,password=oracle)

小結 @ImportResource註解可以用來載入一個外部xml檔案,注入到專案完成配置,但是這樣引入xml並沒有達到SpringBoot簡化配置的目的。

@Configuration和@Bean註解#

@Configuration和@Bean註解並不能讀取配置檔案中的資訊,但是這兩個類本身用來定義配置類

@Configuration用來代替xml檔案,新增在一個類上面

@Bean用來代替bean標籤,宣告在方法上,方法的返回值返回一個物件到Spring的IoC容器中,方法名稱相當於bean標籤中的ID

程式碼樣例

宣告一個bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Vincente
 * @date 2020/07/12-13:28
 * @desc
 **/
@Configuration
public class RestTemplateConfig {
  @Bean
  public RestTemplateConfig restTemplate(){
    return new RestTemplate();
  }
}

測試程式碼

package com.example.test;

import com.example.test.config.RestTemplateConfig;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@SpringBootTest
@RunWith(SpringRunner.class)

class TestApplicationTests {
  @Resource
  private RestTemplateConfig restTemplate;

  @Test
  void testConfigBean(){
    System.out.println(restTemplate);
  }
}

輸出結果

com.example.test.config.RestTemplateConfig@de7e193

@Import註解

@Import註解是用來匯入配置類或者一些需要前置載入的類,帶有@Configuration的配置類(4.2 版本之前只可以匯入配置類,4.2版本之後 也可以匯入 普通類)

程式碼樣例

結合上面的程式碼做修改,不全部貼出

將RestTemplateConfigestTemplateConfig類中的@Configuration註解去掉,在ConfigBean中匯入

@Setter
@Getter
@ToString
@Import(RestTemplateConfig.class)
public class ConfigBean {
  private String dbType;
  private String driverClassName;
  private String host;
  private String userName;
  private String password;
}

測試程式碼

package com.example.test;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@SpringBootTest
@RunWith(SpringRunner.class)

class TestApplicationTests {
  @Resource
  ApplicationContext ctx;

  @Test
  void testConfigBean(){
    System.out.println(ctx.getBean("restTemplate"));
  }
}

輸出結果

com.example.test.config.RestTemplateConfig@6cd15072

小結 可以看到在IoC容器中已經匯入了RestTemplateConfig(普通)類,這個註解類似於之前applicationContext.xml中的import標籤

@ConfigurationProperties和@Value#

@ConfigurationProperties和@Value這兩個註解算是在SpringBoot中用的比較多的註解了,可以在專案的配置檔案application.yml和application.properties中直接讀取配置,但是在用法上二者也是有一定的區別

程式碼樣例

建立配置檔案application.yml

db-config:
 db-type: Oracle
 driver-class-name: jdbc.driver.Ooracle.OracleDriver
 host: 127.0.0.1
 user-name: Oracle
 password: Oracle

server:
 port: 8080

建立配置類ConfigBean

package com.example.test.config;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author Vincente
 * @date 2020/07/12-12:29
 * @desc 配置類
 **/
@Setter
@Getter
@ToString
@ConfigurationProperties(prefix = "db-config")
public class ConfigBean {
  private String dbType;
  private String driverClassName;
  private String host;
  private String userName;
  private String password;
}

測試程式碼

package com.example.test;

import com.example.test.config.ConfigBean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@SpringBootTest
@RunWith(SpringRunner.class)

class TestApplicationTests {
  @Resource
  ConfigBean configBean;
  @Value("${server.port}")
  private String port;

  @Test
  void testConfigBean(){
    System.out.println(configBean);
    System.out.println(port);
  }
}

輸出結果

ConfigBean(dbType=Oracle,driverClassName=jdbc.driver.Ooracle.OracleDriver,userName=Oracle,password=Oracle)
8080

-總結 二者的一些區別

特性 @ConfigurationProperties @Value
SpEL表示式 不支援 支援
屬性鬆散繫結 支援 不支援
JSR303資料校驗 支援 不支援

新增校驗註解

package com.example.test.config;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Null;

/**
 * @author Vincente
 * @date 2020/07/12-12:29
 * @desc 配置類
 **/
@Setter
@Getter
@ToString
@ConfigurationProperties(prefix = "db-config")
@Validated
public class ConfigBean {
  @Null
  private String dbType;
  private String driverClassName;
  private String host;
  private String userName;
  private String password;
}

輸出結果

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'db-config' to com.example.test.config.ConfigBean failed:

  Property: db-config.dbType
  Value: Oracle
  Origin: class path resource [application.yml]:2:12
  Reason: 必須為null

@PropertySource註解

@ConfigurationProperties和@Value這兩個註解預設從專案的主配置檔案中讀取配置,當專案配置較多全部從一個地方讀取會顯得臃腫,可以將配置檔案按照模組拆分讀取到不同的配置類中,可以使用@PropertySource配合@Value讀取其他配置檔案

程式碼樣例

建立配置檔案db-config.yml

/**
 * @author Vincente
 * @date 2020/07/12-14:19
 * @desc
 **/
db-config:
 db-type: Oracle
 driver-class-name: jdbc.driver.Ooracle.OracleDriver
 host: 127.0.0.1
 user-name: Oracle
 password: Oracle

建立配置類ConfigBean

package com.example.test.config;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author Vincente
 * @date 2020/07/12-12:29
 * @desc 配置類
 **/
@Setter
@Getter
@ToString
@PropertySource("classpath:db-config.yml")
@Component
public class ConfigBean {
  @Value("${db-type}")
  private String dbType;
  @Value("${driver-class-name}")
  private String driverClassName;
  @Value("${host}")
  private String host;
  @Value("${user-name}")
  private String userName;
  @Value("${password}")
  private String password;
}

測試程式碼

package com.example.test;

import com.example.test.config.ConfigBean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@SpringBootTest
@RunWith(SpringRunner.class)

class TestApplicationTests {
  @Resource
  ConfigBean configBean;

  @Test
  void testConfigBean(){
    System.out.println(configBean);
  }
}

輸出結果

ConfigBean(dbType=Oracle,userName=Vincente,password=Oracle)

小結

@PropertySource 用於獲取類路徑下的db-config.yml配置檔案,@Value用於獲取yml中的配置資訊,@Component註解用來將配置類交給Spring容器管理

總結

SpringBoot中提供了註解代替配置檔案的方式來獲取專案中的配置,大大簡化了開發,以上總結了常用的讀取配置的方法,簡單來說就是兩種檔案(yml和properties)幾大註解(@Value,@PropertySource,@Configuration,@ConfigurationProperties,@Import,@Bean);首先要了解每個註解的使用場景後,其次根據專案實際情況來具體的使用

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。