springboot 将配置文件中的配置读取为properties配置类
1、确保依赖
1 2 3 4 5 | <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-configuration-processor </artifactId> <optional> true </optional> </dependency> |
2、配置类编写,注意注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.detech.qydxxpt; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author YunguiZheng */ @ConfigurationProperties (prefix = SmsProperties.PREFIX) @Data public class SmsProperties { public static final String PREFIX = "sms" ; /** * 服务地址 */ public String url ; /** * 用户名 */ public String username; /** * 密码 */ public String password ; /** * 固定字符串 */ public String key ; /** * 接入KEY */ public String jrKey ; /** * 业务代码 */ public String ywdm ; } |
3、重要:在启动类上打上注解
@EnableConfigurationProperties(SmsProperties.class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.dflzm.user; import com.detech.qydxxpt.SmsProperties; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableConfigurationProperties (SmsProperties. class ) public class ApiUserCenterApplication { public static void main(String[] args) { SpringApplication.run(ApiUserCenterApplication. class , args); } } |
4、配置文件yml格式如下
1 2 3 4 5 6 7 | sms: url: http: //xxx.com username: xxx password: xxx key: 6798FEB3-71E5-48B6-A49B-1E1F33BAA14B jrKey: "0000088" ywdm: xxx |
5、测试是否配置成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.dflzm.user; import com.detech.qydxxpt.SmsProperties; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith (SpringRunner. class ) @SpringBootTest public class ApiUserCenterApplicationTests { @Resource SmsProperties smsProperties; @Test public void contextLoads() { System.err.println(smsProperties.toString()); } } |
输出:
1 2 | [ 2020 - 06 - 16 10 : 26 : 44 ] [INFO ][com.alibaba.nacos.client.naming: 61 ]-- [LISTENER] adding DEFAULT_GROUP@ @dflzm -backend-service with to listener map SmsProperties(url=http:xxx, username=xxx, password=xxx, key=6798FEB3-71E5-48B6-A49B-1E1F33BAA14B, jrKey=xxx, ywdm=xxx) |
参考:
https://www.cnblogs.com/Guhongying/p/10848251.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了