@PropertySource注解使用详情

您所在的位置:网站首页 注解propertysource @PropertySource注解使用详情

@PropertySource注解使用详情

2023-03-26 00:12| 来源: 网络整理| 查看: 265

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第 7 天,点击查看活动详情

Spring阅读目录

日积月累,水滴石穿 😄

前言

在 Spring Boot中,默认加载的配置文件名称为 application.properties 或者为 application.yml。会将属性文件的值加载到 Spring 的 Environment中。可以使用 @Value 和 @ConfigurationProperties 进行取值。

application.properties 、 application.yml我们可以称为主配置文件,但是在实际开发中,我们不可能将所有的配置放在主配置文件中,日积月累,主配置文件就会越来越庞大,难以维护。所以需要进行拆分,比如数据库配置、redis配置、微信配置,我们可以分别拆分为:datasource.properties(yml) 、redis.properties 、wx.properties。

可惜我们自定义的配置文件并不会被自动加载,我们需要使用 Spring 提供的 @PropertySource 注解,去加载指定的配置文件。

依赖 org.springframework.boot spring-boot-starter-web 2.2.1.RELEASE 复制代码 未使用注解 配置文件

小杰提供了两个配置文件,application.properties和 juejin.properties,文件内容如下:

application.properties blog.name = cxyxj 复制代码 juejin.properties juejin.home = https://juejin.cn/user/2164257578290398 复制代码 测试 @SpringBootApplication(scanBasePackages = "com.cxyxj.propertysource") public class AppMain { public static void main(String[] args) { ConfigurableApplicationContext application = SpringApplication.run(AppMain.class); //获得容器的 Environment Environment env = application.getEnvironment(); //根据key获取 String blog = env.getProperty("blog.name"); String juejin = env.getProperty("juejin.home"); System.out.println(blog); System.out.println(juejin); } } 结果: cxyxj null 复制代码

看到juejin.home并没有被加载到 Environment对象中。

使用注解加载指定properties 启动类加上如上注解,并指定配置文件地址 @PropertySource("juejin.properties") 复制代码 启动结果 cxyxj https://juejin.cn/user/2164257578290398 复制代码 @PropertySource注解定义 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(PropertySources.class) public @interface PropertySource { String name() default ""; String[] value(); boolean ignoreResourceNotFound() default false; String encoding() default ""; Class https://juejin.cn/user/2164257578290398 复制代码 @PropertySource注解修改如下: @PropertySource(name = "juejin", value = {"juejin.xml"}) 复制代码 启动结果 cxyxj https://juejin.cn/user/2164257578290398 复制代码 加载 yml

上面说过,@PropertySource只支持 properties 和 XML 的属性文件格式,这肯定是不行的,现在yml越来越流行了。那如何让@PropertySource支持 yml文件格式呢?这时候就需要使用到 factory属性了,指定 PropertySourceFactory 进行加载。

创建名为 juejin.yml的文件 juejin: home: https://juejin.cn/user/2164257578290398 复制代码 创建YmlPropertyResourceFactory类,并实现 PropertySourceFactory,重写 createPropertySource方法。 import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.DefaultPropertySourceFactory; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.PropertySourceFactory; import java.io.IOException; import java.util.List; import java.util.Optional; /** * 在 @PropertySource 注解的 factory属性指定 YmlPropertyResourceFactory 类,则可以支持读取 yml * @author: cxyxj * @create: 2022-01-21 15:35 */ public class YmlPropertyResourceFactory implements PropertySourceFactory { private static String YML = ".yml"; private static String YAML = ".yaml"; /** * * @param name:@PropertySource 注解 name 的值 * @param resource:资源 */ @Override public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { // 文件名称 String filename = resource.getResource().getFilename(); // 属性源的名称 String resourceName = Optional.ofNullable(name).orElse(filename); if (filename.endsWith(YML) || filename.endsWith(YAML)) { List


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3