栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PropertySourcesPlaceholderConfigurer未在SpringBoot项目中向环境注册

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PropertySourcesPlaceholderConfigurer未在SpringBoot项目中向环境注册

这里的问题是区分

PropertySourcesPlaceholderConfigurer
和
StandardServletEnvironment
,或
Environment
为简单起见。

的

Environment
是,备份整个对象
ApplicationContext
,并且可以解决一束性质(所述
Environment
接口延伸
PropertyResolver
)。A
ConfigurableEnvironment
有一个
MutablePropertySources
您可以通过检索的对象
getPropertySources()
。它
MutablePropertySources
包含要检查
linkedList
的
PropertySource
对象,以解析请求的属性。

PropertySourcesPlaceholderConfigurer
是具有自己状态的单独对象。它拥有自己的
MutablePropertySources
用于解析属性占位符的对象。
PropertySourcesPlaceholderConfigurer
实现,
EnvironmentAware
以便在
ApplicationContext
获取它时为其提供
Environment
对象。在
PropertySourcesPlaceholderConfigurer
添加此
Environment
的
MutablePropertySources
它自己的。然后,它还将添加
Resource
您指定的各种对象
setLocation()
作为附加属性。
这些
Resource
对象不会加入
Environment
的
MutablePropertySources
,因此是不可用
env.getProperty(String)
。

因此,您无法直接将加载

PropertySourcesPlaceholderConfigurer
到的属性
Environment
。你可以做的,而不是直接添加到
Environment
的
MutablePropertySouces
。一种方法是

@PostConstructpublic void setup() throws IOException {    Resource resource = new FileSystemResource("spring.properties"); // your file    Properties result = new Properties();    PropertiesLoaderUtils.fillProperties(result, resource);    env.getPropertySources().addLast(new PropertiesPropertySource("custom", result));}

或干脆(感谢@ M.Deinum)

@PostConstructpublic void setup() throws IOException {    env.getPropertySources().addLast(new ResourcePropertySource("custom", "file:spring.properties")); // the name 'custom' can come from anywhere}

注意,添加a

@PropertySource
具有相同的效果,即。直接添加到中
Environment
,但您是在静态而不是动态地进行操作。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/670656.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号