@value /// @ConfigurationProperties

本文详细介绍如何使用@Value和@ConfigurationProperties注解在SpringBoot中读取配置文件application.properties的自定义配置项,包括ConfigInfo类的创建及注解使用,帮助开发者掌握配置读取技巧。
部署运行你感兴趣的模型镜像

(1) @Value注解
用于逐个读取application.properties中的配置
案例演示
在开发环境的配置文件applicatin-dev.properties中,添加两个自定义配置项school.name和school.website。在idea中可以看到这两个属性不能被SpringBoot识别,背景是桔色的
在这里插入图片描述在这里插入图片描述在这里插入图片描述

在这里插入图片描述

(2) @ConfigurationProperties ( 方式1)

 在com.rdz.springboot.config包下创建ConfigInfo类,并为该类加上Component和ConfigurationProperties注解,prefix可以不指定,如果不指定,那么会去配置文件中寻找与该类的属性名一致的配置,prefix的作用可以区分同名配置

在这里插入图片描述(方式2)
在这里插入图片描述在这里插入图片描述

解决 @ConfigurationProperties 异常在这里插入图片描述

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

`@ConfigurationProperties` 和 `@Value` 注解都是 Spring 中用于从配置文件读取属性值的注解,它们各有特点,可以搭配使用以满足不同的需求。以下是几种常见的搭配书写方式: ### 简单属性和批量属性结合 `@ConfigurationProperties` 适合批量注入配置文件属性,支持松散绑定和复杂类型封装;`@Value` 适合一个一个属性的注入,支持 SpEL 表达式。可以在一个类中同时使用这两个注解。 ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.beans.factory.annotation.Value; @Configuration @ConfigurationProperties(prefix = "myapp") public class AppConfig { private String name; private int port; @Value("${myapp.special.value:defaultValue}") private String specialValue; // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getSpecialValue() { return specialValue; } public void setSpecialValue(String specialValue) { this.specialValue = specialValue; } } ``` ### 配置类和 Bean 方法结合 `@ConfigurationProperties` 可以用在类上,`@Value` 可以用在 `@Bean` 方法的参数上。 ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.beans.factory.annotation.Value; @Configuration @ConfigurationProperties(prefix = "myapp") public class AppConfig { private String name; private int port; // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } @Bean public MyService myService(@Value("${myapp.special.value:defaultValue}") String specialValue) { return new MyService(name, port, specialValue); } } class MyService { private String name; private int port; private String specialValue; public MyService(String name, int port, String specialValue) { this.name = name; this.port = port; this.specialValue = specialValue; } // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getSpecialValue() { return specialValue; } public void setSpecialValue(String specialValue) { this.specialValue = specialValue; } } ``` ### 配置文件示例 ```properties myapp.name=My Application myapp.port=8080 myapp.special.value=customValue ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值