这篇文章主要介绍“Java怎么使用ConfigurationProperties获取yml中的配置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java怎么使用ConfigurationProper
这篇文章主要介绍“Java怎么使用ConfigurationProperties获取yml中的配置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java怎么使用ConfigurationProperties获取yml中的配置”文章能帮助大家解决问题。
我们在开发过程中,会经常遇到需要自定义配置的场景,比如配置一个ip,一个地址等,并将其写入到yml文件中,在项目中使用@Value("${xxxx.xxxx}")来获取自定义的配置,其实是这样是有些笨重的,每定义一个配置,都需要写一个@Value来获取,那为啥不使用一个java config来统一获取配置呢?
编写yml配置文件
user: config: # user_name user-name userName这三种配置方式都可以被识别到 user_name: "zhangsan" age: "20" exmail: "123@123.com" address: "火星"
编写Java config类
// 需要重写get与set方法,此处使用lombok注解来代替@Data// 实例化到spring容器中@Component// 获取前缀为user.config的配置信息,与该类下的属性对比,进行绑定@ConfigurationProperties(prefix = "user.config")public class UserConfig { private String userName; private String age; private String exmail; private String address;}
在需要使用的地方注入
@Resource private UserConfig userConfig;
application.yml
配置类
@Component@ConfigurationProperties(prefix = "system")public class SystemConfig { private static String name; private String version; private String copyrightYear; private boolean demoEnabled; private static String winUploadPath; private static String otherUploadPath; private static boolean addressEnabled; public static String getName() { return name; } public void setName(String name) { SystemConfig.name = name; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getCopyrightYear() { return copyrightYear; } public void setCopyrightYear(String copyrightYear) { this.copyrightYear = copyrightYear; } public boolean isDemoEnabled() { return demoEnabled; } public void setDemoEnabled(boolean demoEnabled) { this.demoEnabled = demoEnabled; } public static String getWinUploadPath() { return winUploadPath; } public static void setWinUploadPath(String winUploadPath) { SystemConfig.winUploadPath = winUploadPath; } public static String getOtherUploadPath() { return otherUploadPath; } public static void setOtherUploadPath(String otherUploadPath) { SystemConfig.otherUploadPath = otherUploadPath; } public static boolean isAddressEnabled() { return addressEnabled; } public void setAddressEnabled(boolean addressEnabled) { SystemConfig.addressEnabled = addressEnabled; } public static String getUploadPath() { OsInfo osInfo = SystemUtil.getOsInfo(); // 判断系统环境获取上传路径 if(ObjectUtils.isNotEmpty(osInfo) && osInfo.iswindows()){ return getWinUploadPath(); }else{ return getOtherUploadPath(); } } public static String getSystemName() { return getName(); }}
name、addressEnabled 以及 winUploadPath、otherUploadPath 都是静态的成员变量,但是他们name、addressEnabled能获取到配置文件的值,winUploadPath、otherUploadPath不可以。
winUploadPath、otherUploadPath对应的ser方法也定义为了静态方法。
关于“Java怎么使用ConfigurationProperties获取yml中的配置”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网精选频道,小编每天都会为大家更新不同的知识点。
--结束END--
本文标题: Java怎么使用ConfigurationProperties获取yml中的配置
本文链接: https://www.lsjlt.com/news/322759.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0