SpringBoot讀取配置文件參數的三種方式


1. 簡介

  在開發中,經常會遇到需要讀取配置文件參數的情況,因此需要開發人員能優雅的讀取配置文件參數值。

2. 配置文件

  在resource目錄下新建配置文件application.yml:

product:
  name: Demo
  author: C3Stones

sys:
  module:
    - code: core
      enable: true
      version: 1
    - code: common
      enable: false
      version: 2

3. @Value 讀取配置文件參數

  • 在Contoller中添加@Value注解
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Value 注解獲取參數值 Controller
 * 
 * @author CL
 *
 */
@RestController
@RequestMapping(value = "value")
public class ValueAnnotationController {

	@Value("${product.name}")
	private String productName;

	@Value("${product.author}")
	private String productAuthor;

	/**
	 * 獲取參數
	 * 
	 * @return
	 */
	@RequestMapping(value = "get")
	public String get() {
		return "產品名稱為:" + productName + ", 作者為:" + productAuthor;
	}

}
# 啟動項目,訪問測試接口
curl http://127.0.0.1:8080/value/get

# 返回
產品名稱為:Demo, 作者為:C3Stones
  • 通過包裝類讀取配置,適合全局使用
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 產品參數
 * 
 * @author CL
 *
 */
@Component
public class ProductProperties {

	/**
	 * 產品名稱
	 */
	@Value("${product.name}")
	private String productName;

	/**
	 * 作者
	 */
	@Value("${product.author}")
	private String productAuthor;

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductAuthor() {
		return productAuthor;
	}

	public void setProductAuthor(String productAuthor) {
		this.productAuthor = productAuthor;
	}

}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.c3stones.config.ProductProperties;

/**
 * Value 注解獲取參數值 Controller
 * 
 * @author CL
 *
 */
@RestController
@RequestMapping(value = "value")
public class ValueAnnotationController2 {

	@Autowired
	private ProductProperties productProperties;

	/**
	 * 獲取參數
	 * 
	 * @return
	 */
	@RequestMapping(value = "get2")
	public String get() {
		return "產品名稱為:" + productProperties.getProductName() + ", 作者為:" + productProperties.getProductAuthor();
	}

}
# 啟動項目,訪問測試接口
curl http://127.0.0.1:8080/value/get2

# 返回
產品名稱為:Demo, 作者為:C3Stones

4. Environment 讀取配置文件參數

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Environment 獲取參數值 Controller
 * 
 * @author CL
 *
 */
@RestController
@RequestMapping(value = "env")
public class EnvironmentController {

	@Autowired
	private Environment environment;

	/**
	 * 獲取參數
	 * 
	 * @return
	 */
	@RequestMapping(value = "get")
	public String get() {
		return "產品名稱為:" + environment.getProperty("product.name") + ", 作者為:" + environment.getProperty("product.author")
				+ ", 版本號為:" + environment.getProperty("product.version", "1.0");
	}

}
# 啟動項目,訪問測試接口
curl http://127.0.0.1:8080/env/get

# 返回
產品名稱為:Demo, 作者為:C3Stones, 版本號為:1.0

5. @ConfigurationProperties 讀取配置文件參數

  • 參數包裝類
import java.util.List;

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

/**
 * 模塊參數
 * 
 * @author CL
 *
 */
@Component
@ConfigurationProperties(prefix = "sys")
@PropertySource(value = { "classpath:application.yml" })
public class ModuleProperties {

	/**
	 * 模塊包裝類
	 */
	private List<Module> module;

	public List<Module> getModule() {
		return module;
	}

	public void setModule(List<Module> module) {
		this.module = module;
	}

	/**
	 * 模塊 Entity
	 * 
	 * @author CL
	 *
	 */
	public static class Module {

		/**
		 * 編碼
		 */
		private String code;

		/**
		 * 是否啟用
		 */
		private Boolean enable;

		/**
		 * 版本
		 */
		private Integer version;

		public Module() {
			super();
		}

		public Module(String code, Boolean enable, Integer version) {
			super();
			this.code = code;
			this.enable = enable;
			this.version = version;
		}

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public Boolean getEnable() {
			return enable;
		}

		public void setEnable(Boolean enable) {
			this.enable = enable;
		}

		public Integer getVersion() {
			return version;
		}

		public void setVersion(Integer version) {
			this.version = version;
		}

		@Override
		public String toString() {
			return "模塊編碼:" + code + " - 是否啟用:" + enable + " - 版本:" + version;
		}

	}
}
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.c3stones.config.ModuleProperties;
import com.c3stones.config.ModuleProperties.Module;

/**
 * ConfigurationProperties 注解獲取參數值 Controller
 * 
 * @author CL
 *
 */
@RestController
@RequestMapping(value = "config")
public class ConfigurationPropertiesController {

	@Autowired
	private ModuleProperties moduleProperties;

	/**
	 * 獲取參數
	 * 
	 * @return
	 */
	@RequestMapping(value = "get")
	public String get() {
		List<Module> module = moduleProperties.getModule();
		return module.toString();
	}

}
# 啟動項目,訪問測試接口
curl http://127.0.0.1:8080/config/get

# 返回
[模塊編碼:core, 是否啟用:true, 版本:1, 模塊編碼:common, 是否啟用:false, 版本:2]

6. 項目地址

  spring-boot-read-properties


免責聲明!

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



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