springBoot加载配置文件
创始人
2024-11-10 02:07:22
0

1. 说明

Spring Boot会自动加载application.propertiesapplication.yml,所放置的位置如下表,所有位置的文件都会被加载(互补配置),高优先级配置内容会覆盖低优先级配置内容。

自动加载配置文件的目录及优先级
位置优先级
项目根目录config文件夹下优先级最高
项目根目录下优先级第二
resources目录中config文件夹下优先级第三
resources目录下优先级最低

2. 获取配置信息

2.1 @Value

(1) 说明

可直接用@Value注解获取已加载指定key的配置数据。

可通过 :defaultValue 设置默认值,不设置默认值时 key 不存在时会抛出异常

(2)示例

    @Value("${key:defaultValue}")     private String demoKey;

2.2 @ConfigurationProperties

(1) 说明

将指定前缀的配置封装到实体类中。有如下属性:

ConfigurationProperties属性
属性说明
value、prefix配置前缀
ignoreInvalidFields默认是false,当绑定时候出现错误是否要忽略,这种错误一般是类型转换错误
ignoreUnknownFields默认是true,会忽略掉配置文件里未绑定到实体里的配置

(2)示例

@Component @ConfigurationProperties(prefix="demo") public class DemoProperties {     // 获取demo.a1     private String a1;     // 获取demo.a2     private String a2;          //todo set/get方法 }

2.3 Environment

(1) 说明

是一个提供访问环境变量的类常用方法有:

方法说明
getProperty(String key)
getProperty(String key, String defaultValue)
getProperty(String key, Class targetType)
getProperty(String key, Class targetType, T defaultValue)
获取指定key的配置信息
可设置默认值或数据类型
getRequiredProperty(String key)
getRequiredProperty(String key, Class targetType)
获取指定key的必要配置信息,不存在会抛异常。
resolvePlaceholders(String text)通过占位符获取配置信息。例如:${key}
resolveRequiredPlaceholders(String text)通过占位符获取必要配置信息,不存在会抛异常。
getActiveProfiles()获取当前的环境配置
getDefaultProfiles()获取默认的环境配置
acceptsProfiles(Profiles profiles)判断是否激活指定环境配置

(2)示例

    @Autowired     Environment environment;     @Test     void contextLoads2() throws Exception {         System.out.println(environment.getProperty("demo.k1"));     }

3. 引入外部配置文件

3.1 @PropertySource

(1) 说明

用于指定资源文件读取的位置,它不仅能读取properties文件,也能读取xml文件,并且还可通过YAML解析器,配合自定义PropertySourceFactory实现解析YAML文件。

属性说明
name该PropertySource的名字,如果不指定会用文件名按照一定的方式生成
value指定扫描文件的路径,可以配置多个
ignoreResourceNotFound当资源不存在时,是否忽略,默认不忽略,也就是会报错。
encoding指定文件编码
factory指定文件解析工厂,可用于解析不同类型的配置文件,如 yml

(2)示例

@Component @PropertySource(value = "classpath:demo.properties",encoding = "utf-8") public class Config { }

3.2 PropertySourcesPlaceholderConfigurer

(1) 说明

读取外部配置文件,并将配置文件中的属性值注入到应用程序中。

(2)示例

    /**      * 加载yml配置文件      */     @Bean     public static PropertySourcesPlaceholderConfigurer yamlConfigurer() {         PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();         YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();         yaml.setResources(new ClassPathResource("demo.yml"));         configurer.setProperties(Objects.requireNonNull(yaml.getObject()));         return configurer;     }     /**      * 加载properties配置文件      */     @Bean     public PropertySourcesPlaceholderConfigurer propertiesConfigurer() {         PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();         configurer.setLocations(new ClassPathResource("demo.properties"));         return configurer;     }

3.3 直接加载并获取配置信息

(1) 说明

Properties是一个Map体系集合类,继承于Hashtable。主要用于读取Java的properties配置文件。

注:也可使用其他类库直接读取配置文件。

(2)示例

InputStream ips = new FileInputStream("config.properties"); Properties props = new Properties(); props.load(ips); ips.close(); String value= props.getProperty("key");

3.4 命令行参数

(1) 说明

可以通过 --spring.config.location参数来指定配置文件的位置。

可以通过 --spring.config.name 参数指定配置文件名称。

注:也可 使用 --app.key=value  修改配置文件中的配置

(2)示例

// 加载 /path/config/ 下的myconfig.properties或myconfig.yml配置文件 java -jar app.jar --spring.config.location=file:/path/config/  --spring.config.name=myconfig

4. 分环境加载配置文件

4.1 默认加载的配置文件

(1) 说明

可通过 application-{profile}.properties 或application-{profile}.yml的方式配置多个配置文件。

注:application.properties 或application.yml可配置公共配置数据。

4.2 @Profile

(1) 说明

常和@Component、@Configuration、@Bean注解一块使用,用于标明需要spring管理类是否需要管理的环境。

注:只有配置了spring.profiles.active 或 spring.profiles.default 时@Profile才会生效。

(2)示例

//Config类只有在dev环境时由spring管理 //可通过配置多个不同环境的相同类来做到分环境加载配置 @Component @Profile("dev") public class Config {     //DataSource类只有在test环境时由spring管理     @Bean     @Profile("test")     public DataSource getTestDataSource () {         return null;     } }

4.3 引入外部配置文件时自定义不同环境的文件名

   @Bean     public PropertySourcesPlaceholderConfigurer configurer1(Environment environment) {         PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();         String[] activeProfiles = environment.getActiveProfiles();         if (activeProfiles != null && activeProfiles.length > 0) {             YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();             ClassPathResource[] resources=new ClassPathResource[activeProfiles.length];             for (int i = 0; i < activeProfiles.length; i++) {                 resources[i]=new ClassPathResource("demo-" + activeProfiles[i] + ".yml");             }             yaml.setResources(resources);             configurer.setProperties(Objects.requireNonNull(yaml.getObject()));         }         return configurer;     }     @Bean     public PropertySourcesPlaceholderConfigurer configurer2(Environment environment) {         PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();         String[] activeProfiles = environment.getActiveProfiles();         if (activeProfiles != null && activeProfiles.length > 0) {             for (int i = 0; i < activeProfiles.length; i++) {                 configurer.setLocations(new ClassPathResource("demo-" + activeProfiles[i] + ".properties"));             }         }         return configurer;     }

5. 配置文件常用语法

 (1)可使用${key}获取其他配置数据

相关内容

热门资讯

秒懂普及”珊瑚互娱房卡领取码“... 秒懂普及”珊瑚互娱房卡领取码“王者大厅房间卡怎么购买游戏中心打开微信,添加客服【113857776】...
秒懂教程!我买微信牛牛房卡链接... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:71319951许多玩家在游戏中会购买房卡来享受...
正规平台有哪些,怎么买斗牛房卡... 微信游戏中心:火神大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
给大家讲解“购买斗牛房卡联系方... 起点大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
ia攻略/游戏推荐斗牛房卡出售... ia攻略/游戏推荐斗牛房卡出售长虹大厅/科技房卡多少钱一张Sa9Ix苹果iPhone 17手机即将进...
一分钟实测分享”时光互娱低价获... 第二也可以在游戏内商城:在游戏界面中找到 “微信金花,斗牛链接房卡”“商城”选项,选择房卡的购买选项...
一分钟了解!牛牛房卡游戏平台加... 微信游戏中心:乐乐堂房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程序...
正规平台有哪些,金花房卡是正规... 海航大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
秒懂教程!微信牛牛房间怎么弄,... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:66336574许多玩家在游戏中会购买房卡来享受...
终于找到“微信怎样开炸金花房间... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
秒懂百科”海洋世界哪里有详细房... 哪里有详细房卡介绍是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:113857776许多玩家在游戏中...
头条推荐!游戏推荐斗牛房卡出售... 头条推荐!游戏推荐斗牛房卡出售新神兽/流樱大厅/微信链接房间卡怎么购买新神兽/流樱大厅是一款非常受欢...
推荐一款!怎么买斗牛房卡朱雀大... 微信游戏中心:朱雀大厅房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或相关小程...
玩家攻略,牛牛房卡代理荣耀联盟... 荣耀联盟是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
IA解析/牛牛房卡游戏平台加盟... IA解析/牛牛房卡游戏平台加盟天道联盟/随意玩/微信链接房卡卖家联系方式Sa9Ix苹果iPhone ...
分享经验”百万牛哪里买低价获取... 分享经验”百万牛哪里买低价获取“新老夫子房卡充值游戏中心打开微信,添加客服【113857776】,进...
一分钟了解!金花房卡出售大众互... 今 日消息,大众互娱房卡添加微信33549083 苹果今日发布了 iOS 16.1 正式版更新,简单...
秒懂教程!微信买链接拼三张房卡... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:56001354许多玩家在游戏中会购买房卡来享...
房卡必备教程“购买金花房卡联系... 新琉璃金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
1分秒分析”茄子娱乐房卡详细充... 第二也可以在游戏内商城:在游戏界面中找到 “微信金花,斗牛链接房卡”“商城”选项,选择房卡的购买选项...