Spring Boot读取properties配置文件中的数据

Spring Boot读取properties配置文件中的数据JavaEE 目录 https blog csdn net dkbnull article details 87932809Spri 专栏 https blog csdn net dkbnull category 9278145 htmlSpringCl 专栏 https blog csdn net dkbnull category 9287932 ht

Java EE 目录:https://blog.csdn.net/dkbnull/article/details/

Spring Boot 专栏:https://blog.csdn.net/dkbnull/category_9278145.html

Spring Cloud 专栏:https://blog.csdn.net/dkbnull/category_9287932.html

 

 

Spring Boot最常用的3种读取properties配置文件中数据的方法:

1、使用@Value注解读取

读取properties配置文件时,默认读取的是application.properties。

application.properties:

demo.name=Name demo.age=18

Java代码:

import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GatewayController { @Value("${demo.name}") private String name; @Value("${demo.age}") private String age; @RequestMapping(value = "/gateway") public String gateway() { return "get properties value by ''@Value'' :" + //1、使用@Value注解读取 " name=" + name + " , age=" + age; } }

运行结果如下:

Spring Boot读取properties配置文件中的数据

这里,如果要把

 @Value(“${demo.name}”)
            

private String name;
            

@Value(“${demo.age}”)
            

private String age;

部分放到一个单独的类A中进行读取,然后在类B中调用,则要把类A增加@Component注解,并在类B中使用@Autowired自动装配类A,代码如下。

类A:

import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ConfigBeanValue { @Value("${demo.name}") public String name; @Value("${demo.age}") public String age; }

类B:

import cn.wbnull.springbootdemo.config.ConfigBeanValue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GatewayController { @Autowired private ConfigBeanValue configBeanValue; @RequestMapping(value = "/gateway") public String gateway() { return "get properties value by ''@Value'' :" + //1、使用@Value注解读取 " name=" + configBeanValue.name + " , age=" + configBeanValue.age; } } 

运行结果如下:

Spring Boot读取properties配置文件中的数据

注意:如果@Value${}所包含的键名在application.properties配置文件中不存在的话,会抛出异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘configBeanValue’: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘demo.name’ in value “${demo.name}”

 

2、使用Environment读取

application.properties:

demo.sex=男 demo.address=山东 

Java代码:

import cn.wbnull.springbootdemo.config.ConfigBeanValue; 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; @RestController public class GatewayController { @Autowired private ConfigBeanValue configBeanValue; @Autowired private Environment environment; @RequestMapping(value = "/gateway") public String gateway() { return "get properties value by ''@Value'' :" + //1、使用@Value注解读取 " name=" + configBeanValue.name + " , age=" + configBeanValue.age + "

get properties value by ''Environment'' :" + //2、使用Environment读取 " , sex=" + environment.getProperty("demo.sex") + " , address=" + environment.getProperty("demo.address"); } }

运行,发现中文乱码:

Spring Boot读取properties配置文件中的数据

这里,我们在application.properties做如下配置:

server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true spring.messages.encoding=UTF-8 

然后修改IntelliJ IDEA,File –> Settings –> Editor –> File Encodings ,将最下方Default encoding for properties files设置为UTF-8,并勾选Transparent native-to-ascii conversion。

Spring Boot读取properties配置文件中的数据

重新运行结果如下:

Spring Boot读取properties配置文件中的数据

 

3、使用@ConfigurationProperties注解读取

在实际项目中,当项目需要注入的变量值很多时,上述所述的两种方法工作量会变得比较大,这时候我们通常使用基于类型安全的配置方式,将properties属性和一个Bean关联在一起,即使用注解@ConfigurationProperties读取配置文件数据。

在src\main\resources下新建config.properties配置文件:

demo.phone=10086 demo.wife=self 

创建ConfigBeanProp并注入config.properties中的值:

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "demo") @PropertySource(value = "config.properties") public class ConfigBeanProp { private String phone; private String wife; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } } 

@Component 表示将该类标识为Bean

@ConfigurationProperties(prefix = “demo”)用于绑定属性,其中prefix表示所绑定的属性的前缀。

@PropertySource(value = “config.properties”)表示配置文件路径。

 

使用时,先使用@Autowired自动装载ConfigBeanProp,然后再进行取值,示例如下:

import cn.wbnull.springbootdemo.config.ConfigBeanProp; import cn.wbnull.springbootdemo.config.ConfigBeanValue; 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; @RestController public class GatewayController { @Autowired private ConfigBeanValue configBeanValue; @Autowired private Environment environment; @Autowired private ConfigBeanProp configBeanProp; @RequestMapping(value = "/gateway") public String gateway() { return "get properties value by ''@Value'' :" + //1、使用@Value注解读取 " name=" + configBeanValue.name + " , age=" + configBeanValue.age + "

get properties value by ''Environment'' :" + //2、使用Environment读取 " sex=" + environment.getProperty("demo.sex") + " , address=" + environment.getProperty("demo.address") + "

get properties value by ''@ConfigurationProperties'' :" + //3、使用@ConfigurationProperties注解读取 " phone=" + configBeanProp.getPhone() + " , wife=" + configBeanProp.getWife(); } }

运行结果如下:

Spring Boot读取properties配置文件中的数据

 

 

GitHub:https://github.com/dkbnull/SpringBootDemo

微信:https://mp.weixin..com/s/swtkNq6CLMsP4uc4PgaVHg

微博:https://weibo.com/ttarticle/p/show?id=

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/199126.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午1:48
下一篇 2026年3月26日 下午1:48


相关推荐

  • 快速理解宽带调频中的贝塞尔函数

    快速理解宽带调频中的贝塞尔函数相信很多没有学过数学物理方法的同学对宽带调频中的第一类贝塞尔函数的应用会有些许异或 我在刚学习的时候也是如此 查了一下网上写的都是比较难以理解的方法 用了好就我才理解 所以也想写一个适合没有这个特殊函数基础的童鞋对这个函数的快速理解 下面是我的见解 大佬勿喷 一 第一类贝塞尔函数公式和图形如下

    2026年3月17日
    2
  • python中的取整(python向上取整函数)

    importmatha=3/2print(a,type(a))a=math.ceil(a)print(a,type(a))输出结果:

    2022年4月12日
    45
  • 物联网开发_物联网是不是一种骗局

    物联网开发_物联网是不是一种骗局物联网期末大作业—睡眠质量检测系统

    2022年10月9日
    4
  • 组件图(构件图)

    组件图(构件图)一 定义 1 组件图又称为构件图 ComponentDia 组件图中通常包括组件 接口 以及各种关系 组件图显示组件以及它们之间的依赖关系 它可以用来显示程序代码如何分解成模块或组件 一般来说 组件就是一个实际文件 可以有以下几种类型 nbsp nbsp 源代码组件 一个源代码文件或者与一个包对应的若干个源代码文件 nbsp nbsp 二进制组件 一个目标码文件 一个静态的或者动态的库文件

    2026年3月20日
    2
  • 将 Linux 移植到 M1 Mac 真的太难了!「建议收藏」

    将 Linux 移植到 M1 Mac 真的太难了!「建议收藏」【CSDN编者按】自去年苹果自研M1芯片发布之后,激发了无数用户的体验热情,与此同时,也吸引大批开发者在M1上开启探索模式。其中,国外一位资深操作系统移植专家HectorMa…

    2026年3月4日
    5
  • 安卓浏览器横评_flash浏览器

    安卓浏览器横评_flash浏览器近日国外媒体AndroidAuthority就对全球使下载最大的11款手机浏览器进行了性能方面的横向评测,相信在看完后,你能够从中找到更适合自己的手机浏览器。(注:本测试主要针对浏览器性能,并不能说明浏览器的整体效果)一、测试设备、工具及浏览器介绍1、测试设备Nexus4,运行Android4.4.2系统。2、测试浏览器本次测试的浏览器总共11款,分别是:-百度浏览器3.1.0.2(国际…

    2025年8月12日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号