SpringBoot读取配置文件参数的三种方式

您所在的位置:网站首页 获取bean几种方式 SpringBoot读取配置文件参数的三种方式

SpringBoot读取配置文件参数的三种方式

#SpringBoot读取配置文件参数的三种方式| 来源: 网络整理| 查看: 265

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; public List getModule() { return module; } public void setModule(List 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 = 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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3