Spring Boot相关:@ConfigurationProperties@Value读取配置文件的对比

news/2025/6/19 15:21:30
对比点@ConfigurationProperties@Value
底层框架Spring BootSpring
功能批量注入配置文件中的属性单个注入
属性setXX()方法需要不需要
复杂类型属性注入支持不支持
松散绑定支持不支持
JSR303数据校验支持不支持
SpEL表达式不支持支持

 Spring Boot可以读取默认名称的配置文件,如application.properties,application.yaml。但如果文件名是其他,则需要指定。添加如下注解

如:@PropertySource("classpath:mysql.properties")

 

通常:@Component 等价于 @Configuration + @EnableConfigurationProperties

1.使用@ConfigurationProperties读取

 配置信息:

#配置student属性  
student.id = 10
student.name=xiaoming
student.sex=F#以,分割传递 数组集合
student.hobby=football,pingpong,sing#传递 map 参数
#前缀    属性名 key = value
student.course.math=89
student.course.english=99student.email=wp@163.com

javabean:必须有setter方法

@Component  //将对象加入容器
@ConfigurationProperties(prefix = "student")  //读取默认配置文件中的student开头的属性
@Validated  //开启属性校验
public class Student {private int id;private String name;private String sex;/*** 兴趣爱好*/private List<String> hobby;/*** 成绩*/private Map course;@Emailprivate String email;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public List<String> getHobby() {return hobby;}public void setHobby(List<String> hobby) {this.hobby = hobby;}public Map getCourse() {return course;}public void setCourse(Map course) {this.course = course;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", sex='" + sex + '\'' +", hobby=" + hobby +", course=" + course +", email='" + email + '\'' +'}';}
}

2.使用@value读取

setter可省略

@Component  //将对象加入容器
@Validated  //开启属性校验
public class Student2 {@Value("#{2*8}") //@Value("#{2*8}")可以在#{}进行运算,并直接赋值  SpEL表达式private int id;@Value("#{student.name}")//这样也可以啊!private String name;@Value("${student.sex}")private String sex;/*** 兴趣爱好*///@Value("${student.hobby}")  // 复杂类型不支持private List<String> hobby;/*** 成绩*///@Value("${student.course}") // 复杂类型不支持private Map course;@Email@Value("${student.email}")private String email;@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", sex='" + sex + '\'' +", hobby=" + hobby +", course=" + course +", email='" + email + '\'' +'}';}
}

 

文章来源:https://blog.csdn.net/zitian246/article/details/109588383
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-3440601.html

相关文章

MySQL索引原理以及类型

1、什么是索引 索引是在MySQL的存储引擎上&#xff0c;对其表中的某个列或多列通过一些算法实现可快速查询出结果的一种方法。 2、为什么要有索引 就像一本书要有目录一样&#xff0c;我们可快速通过目录来查找对应的章节得出结果。索引在MySQL中也叫做“键”&#xff0c;是存储…

python之input()、while、title()和upper()

代码举例&#xff1a; # 小应用&#xff1a;问卷调查&#xff0c;记录下调查者名字和回答&#xff0c;询问是否继续。 # 运用数据字典、while、input()、title()和upper()。 responses {} flag True while flag:name input("\n请输入姓名&#xff1a;")answer in…

Spring Boot相关:@Configuration @Bean 将类加入容器

Configuration &#xff1a;标记当前类是配置类 Bean&#xff1a;将当前方法创建类 并放入容器中&#xff0c;相当与bean.xml种的<bean> 通常&#xff1a;Component 等价于 Configuration EnableConfigurationProperties 在springboot 将类放置在容器中 1.常用的注…

常用接口文档模板

接口规范说起来大&#xff0c;其实也就那么几个部分&#xff0c;接口规范、接口管理工具、接口文档编写、开发文档编写。以下将详细介绍&#xff0c;下面进入正文&#xff1a; 接口规范文档具体内容如下&#xff1a;一&#xff1a;协议规范二&#xff1a;域名规范三&#xff1a…

$emit使用报错处理

在Vue SFC模式中&#xff0c;父元素传值给子组件采用&#xff1a;props 子组件传值到父元素需要用到$emit。 当然&#xff0c;可以用store实现&#xff08;这里记录一下遇到的坑&#xff09; 由于没有仔细阅读Vue的文档&#xff0c;遇到了一个最基础的问题&#xff0c;导出程序…

怎么在myeclipse中怎么集成Tomcat。

运行myeclipse软件,创建web工程项目,在myeclipse软件的上面选择Deploy and undeploy J2EE projects. 部署或者卸载j2ee项目工程,如同所示,点击下面的图标,就会出现.2 然后我们选择要部署的项目,点击添加按钮. 我们点击add按钮,这个时候就要为项目配置以哪种方式打开,这里我们选…

Spring Boot相关:引入Mybatis

1.引入依赖 <!-- 阿里巴巴的Druid数据源依赖启动器 --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><!-- MyBatis依赖启…

文摘:DataFrame数据清洗

原文地址&#xff1a;https://www.zybuluo.com/jk88876594/note/802632 DataFrame——数据清洗 阿雷边学边教python数据分析第3期——pandas与numpy #导入pandas库和numpy库import pandas as pdimport numpy as np1.缺失值处理 python中用NaN(Not a Number)表示缺失数据 #示例数…

Spring Boot相关:dao层注入时属性名飘红的情况起别名后,resultType仍飘红解决方案

解决方案与问题来源&#xff1a;https://blog.csdn.net/zitian246/article/details/109608796

concurrent.futures用法

submit 异步提交map(func, *iterables, timeoutNone, chunksize1) 类似 map(func,*iterables)除了以下&#xff1a;迭代器是立即收集的而不是延迟收集的func是异步执行的和对func的多个调用可以并发执行shutdown(waitTrue) 相当于进程池的pool.close()pool.join()操作waitTru…