SpringBoot如何获取application.properties中自定义的值
发表于:2025-11-12 作者:千家信息网编辑
千家信息网最后更新 2025年11月12日,这篇文章主要讲解了"SpringBoot如何获取application.properties中自定义的值",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"
千家信息网最后更新 2025年11月12日SpringBoot如何获取application.properties中自定义的值
这篇文章主要讲解了"SpringBoot如何获取application.properties中自定义的值",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"SpringBoot如何获取application.properties中自定义的值"吧!
目录结构:
pom文件:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.4 com.example i18nSpringbootDemo-1 0.0.1-SNAPSHOT i18nSpringbootDemo-1 Demo project for Spring Boot 11 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-starter-validation org.springframework.boot spring-boot-maven-plugin war
application.properties:
test.user.id=12#也可以写成 test.user.user-name=zhangsantest.user.userName=zhansan#也可以写成 test.user.user-password=XXXtest.user.userPassword=PWD123#也可以写成 test.user.la-big-decimal=XXXtest.user.laBigDecimal=138.3test.user.maps.key1=V1test.user.maps.key2=123test.user.lists=a12,a13,sdftest.user.department.dep-code=dep001test.user.department.dep-name=depName001
Department类:
package com.example.demo.obj; public class Department { private String depCode; private String depName; /** * @return depCode */ public String getDepCode() { return depCode; } /** * @param depCode セットする depCode */ public void setDepCode(String depCode) { this.depCode = depCode; } /** * @return depName */ public String getDepName() { return depName; } /** * @param depName セットする depName */ public void setDepName(String depName) { this.depName = depName; } @Override public String toString() { return "Department [depCode=" + depCode + ", depName=" + depName + "]"; } }User类:
package com.example.demo.obj; import java.math.BigDecimal;import java.util.List;import java.util.Map; import javax.validation.constraints.Email;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;import org.springframework.validation.annotation.Validated; /* * 将配置文件的每一个属性值,映射到这个组件中: * ①@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定; * prefix = "test.user":将配置文件中前缀为test.user下面的所有属性进行一一映射 * 只有这个组件是容器中的组件,才能提供@ConfigurationProperties的功能,所以要加@Component * * ②@Value("${key}")从环境变量、配置文件中获取值 * @Value("#{SpEl}")表达式 * * @ConfigurationProperties与@Value的区别: * @ConfigurationProperties支持松散语法,JSR303数据校验,复杂类型封装,不支持SpEL * @Value支持SpEL,不支持松散语法,JSR303数据校验,复杂类型封装 * 如果说,我们在某个业务逻辑中需要获取一下配置文件中的某项值,可以用@Value * 如果说,我们专门编写了一个javaBean去和配置文件进行映射,我们直接使用@ConfigurationProperties */@Component@ConfigurationProperties(prefix = "test.user")@Validatedpublic class User { //@Value("#{10*2}") private Integer id; //@Email userName必须输入邮箱格式的值,要不然报错 //@Value("${test.user.userName}") private String userName; //@Value("${test.user.userPassword}") private String userPassword; //@Value("${test.user.laBigDecimal}") private BigDecimal laBigDecimal; //@Value("${test.user.maps}") X 不行会报错 private Map maps; //@Value("${test.user.lists}") private List I18nSpringbootDemo1Application类:
package com.example.demo; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 应用启动类 * */@SpringBootApplicationpublic class I18nSpringbootDemo1Application { public static void main(String[] args) { SpringApplication.run(I18nSpringbootDemo1Application.class, args); } }单元测试类I18nSpringbootDemo1ApplicationTests:
package com.example.demo; import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest; import com.example.demo.obj.User; @SpringBootTestclass I18nSpringbootDemo1ApplicationTests { @Autowired User user; @Test void contextLoads() { System.out.println(user.toString()); } }启动:
结果:
User [id=12, userName=zhansan, userPassword=PWD123, laBigDecimal=138.3, maps={key1=V1, key2=123}, lists=[a12, a13, sdf], department=Department [depCode=dep001, depName=depName001]]
感谢各位的阅读,以上就是"SpringBoot如何获取application.properties中自定义的值"的内容了,经过本文的学习后,相信大家对SpringBoot如何获取application.properties中自定义的值这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
文件
配置
支持
属性
组件
学习
不行
复杂
松散
内容
数据
类型
语法
会报
封装
业务
前缀
功能
单元
变量
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
海南海事局网络安全等级保护
北京建行服务器出错
密云区正规软件开发概况
网络安全团会感想
浙江信息软件开发介绍
怎么看服务器电源好坏
网络安全动态视频
英雄联盟进服务器排队哪一年
关键数字网络技术
威科法律百科全书数据库
北京环宇网络技术有限公司
网络安全现状分析师
管家婆数据库备份文件在哪里
嵌入式软件开发调试系统
云服务器不支持hyper
软件开发者感悟
护苗网络安全课堂的观后感
表格中如何隐藏零的数据库
网络安全攻防实验室
数据库分页查询如何实现
研发软件开发测试工程师
5e和完美服务器有什么区别
福建网络软件开发电话多少
上海网络安全检查工作
转变网络安全监管理念
奉贤区个人软件开发不二之选
u8取消启用 数据库
wps最多放多少条数据库
数据库概论重点
统战领域网络安全