springboot怎么通过@Value,@ConfigurationProperties获取配置
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,本篇内容主要讲解"springboot怎么通过@Value,@ConfigurationProperties获取配置",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大
千家信息网最后更新 2025年02月23日springboot怎么通过@Value,@ConfigurationProperties获取配置
本篇内容主要讲解"springboot怎么通过@Value,@ConfigurationProperties获取配置",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot怎么通过@Value,@ConfigurationProperties获取配置"吧!
通过@Value,@ConfigurationProperties获取配置
spring boot 获取配置项值
使用版本是1.5.4
举例一个线程池的配置:
在application.yml添加配置项及值
# 线程池配置 taskexecutor: corePoolSize: 5 maxPoolSize: 10 queueCapacity: 25
通过@Value 获取值
@Configuration@EnableAsyncpublic class ExecutorConfig { @Value("${taskexecutor.corePoolSize}") private int corePoolSize; @Value("${taskexecutor.maxPoolSize}") private int maxPoolSize; @Value("${taskexecutor.queueCapacity}") private int queueCapacity; @Bean public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setThreadNamePrefix("TaskExecutor-"); executor.initialize(); return executor; } }
通过@ConfigurationProperties 获取值
@Configuration @EnableAsync @ConfigurationProperties(ignoreUnknownFields = false,prefix = "taskexecutor") public class ExecutorConfig { private int corePoolSize; private int maxPoolSize; private int queueCapacity; @Bean public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setThreadNamePrefix("TaskExecutor-"); executor.initialize(); return executor; } }
通过@ConfigurationProperties加载配置文件,将配置项与bean及属性关联,指定ignoreUnknownFields当有属性未匹配到值时会抛出异常,用prefix指定配置项的前缀。
@ConfigurationProperties还支持层级结构、 布尔、集合等类型的值注入
说下@ConfigurationProperties和@Value区别
@Configuration | @Value | |
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SPEL语法 | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
配置文件yml还是properties他们都能获取到值;
如果说, 只是在某个业务逻辑中需要获取一项配置文件中的某项值, 使用@Value
如果说,专门编写了一个javaBean 来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
配置文件注入值数据校验
@Component@ConfigurationProperties(prefix = "person")@Validatedpublic class Person { /** ** */ //Value("${person.last-name}") //lastName必须为邮箱格式 @Email private String lastName; //@Value("#{11*2}") private Integer age; //@Value("true") private Boolean boss; private Date birth; private Map* maps; private List
到此,相信大家对"springboot怎么通过@Value,@ConfigurationProperties获取配置"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
配置
支持
文件
属性
松散
内容
数据
类型
线程
语法
学习
复杂
实用
更深
业务
兴趣
前缀
功能
只是
实用性
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
常见保证网络安全公示
福州韵诗网络技术公司
苹果手机视频软件开发
海量流式数据数据库技术
维普数据库论文检测
嘉兴市星玲网络技术有限公司
恒深互联网科技科技指数
机房网络安全岗位安全制度
国内专业软件开发公司排名
哪个不是网络安全防范措施()
日新软件开发有限公司
一点网络技术有限公司
泛微服务器维修公司地址电话
古交软件开发哪里好
国旗下网络安全教育教案
数据库中间层和中间件的区别
网络安全日组织
阿里云 服务器 带宽
代理服务器搭建多少钱
安徽省新盟市网络技术有限公司
百鸟网络技术公司
服务器上数据库启动命令
计算机网络技术信息咨询
手机的云服务器是什么意思
数据库个体工商户所得税
广西华为云服务器在哪里
计算机网络技术可能适合职业
软件开发动态时序图
美团服务器错误504怎么解决
华为云服务器价格8核