当前位置: 首页 > news >正文

SpringBoot从配置文件中获取属性的方法

方式一:@Value

基本类型属性注入,直接在字段上添加@Value("${xxx.xxx}")即可.注意这里用的是$,而不是#,@Value注入的属性,一般其他属性没有关联关系。

配置文件

user:name: Manaphyage: 19sex: male
@RestController
public class ConfigPropertiesController {@Value("${user.name}")private String name;@Value("${user.age}")private Integer age;@Value("${user.sex}")private String sex;@GetMapping("/user")public String getUser() {return "{name:" + name + ",age:" + age + ",sex:" + sex + "}";}
}

方式二:@ConfigurationProperties

配置文件

person:lastName: helloage: 18boss: falsebirth: 2017/12/12maps: {k1: v1,k2: v2}lists:- lisi- wangwudog:name: 小狗age: 12

JavaBean

/*** 将配置文件中配置的每一个属性的值,映射到这个组件中* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;* prefix = "person":配置文件中哪个下面的所有属性进行一一映射* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能*/
@Component
@ConfigurationProperties(prefix = "person")
@Data
public class Person {private String lastName;private Integer age;private Boolean boss;private Date birth;private Map<String, Object> maps;private List<Object> lists;private Dog dog;}@Data
class Dog {private String name;private Integer age;
}

Controller层

@RestController
public class PersonController {@Autowiredprivate Person person;@GetMapping("/person")public Person getPerson() {return person;}
}

运行结果如下
在这里插入图片描述

我们可以导入配置文件处理器,以后编写配置就有提示了

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>

注意:使用@ConfigurationProperties注入属性时如果只给属性提供get方法,会报错

/*** yml配置*/
sms:region-id: cn-shanghaiaccess-key-id: 123access-key-secret: 123sign-name: 叮咚买菜/*** 实体类*/
@Getter
@Component
@ConfigurationProperties(prefix = "sms")
public class SmsProperties {private String regionId;private String accessKeyId;private String accessKeySecret;
}/*
报错
Description:Failed to bind properties under 'sms' to com.example.producer.producerdemo.util.SmsProperties:Property: sms.access-key-idValue: 123Origin: class path resource [application.yml] - 51:18Reason: java.lang.IllegalStateException: No setter found for property: access-key-idAction:Update your application's configuration
*/

@Value和@ConfigurationProperties比较

@ConfigurationProperties@Value
功能批量注入配置文件中的属性一个个指定
松散绑定(松散语法)支持不支持
SpEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持

配置文件yml还是properties他们都能获取到值;

如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;

如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties。

http://www.lryc.cn/news/273626.html

相关文章:

  • oracle物化视图
  • 基于ssm校园线上订餐系统的设计与实现论文
  • 鸿蒙南向开发—OpenHarmony技术编译构建框架
  • Android Jetpack学习系列——Navigation
  • 编程语言的新趋势
  • C++:类和对象(2)
  • 【React系列】网络框架axios库的使用
  • pygame学习(二)——绘制线条、圆、矩形等图案
  • TCL学习笔记(持续更新)
  • Xpath的问题:为什么在DOM中确定存在(可见)的元素,用//表达式匹配不到(附解决办法)
  • 有没有游泳可以戴的耳机?游泳耳机入耳式好,还是骨传导好
  • 【绘图软件】自用安装教程
  • AIGC时代-GPT-4和DALL·E 3的结合
  • springBoot集成RabbitMQ实现(直连模式\路由模式\广播模式\主题模式)的消息发送和接收
  • Attention机制
  • Rust 常用的第三方库
  • 构建高可用性Java应用:介绍分布式系统设计与开发
  • x-cmd pkg | gitui - git 终端交互式命令行工具
  • javaWeb案例知识点
  • SQL日期列更新操作详解
  • stable diffusion 基础教程-图生图
  • 如何获取高质量的静态住宅代理?常见问题与误区
  • 基于SpringBoot的旅游网站281
  • 做外贸没客户就静下来沉淀
  • 网络流总结
  • 安卓11通过脚本修改相应板型系统属性
  • 网络安全—PKI公钥基础设施
  • 推荐一款加速器,也可加速github
  • springboot框架,中间库是mognodb,可以写入2个数据库的Demo
  • 基于Java SSM框架实现旅游资源网站系统项目【项目源码+论文说明】