【问题标题】:What is the difference between loading properties files in spring through @propertysource annotation and by using PropertySourcesHolderConfigure bean在spring中通过@propertysource注解和使用PropertySourcesHolderConfigure bean加载属性文件有什么区别
【发布时间】:2019-03-04 01:48:24
【问题描述】:

我正在研究如何在 Spring 中访问属性文件,我很困惑地找出 <context:property-placeholder> vs PropertySourcesPlaceHoderConfigure vs @PropertySource vs PropertyPlaceHolderConfigure 之间的区别。

【问题讨论】:

    标签: java spring properties-file spring-annotations


    【解决方案1】:

    来自PropertySourcesPlaceholderConfigurer docs

    PropertySourcesPlaceholderConfigurer 更加灵活,可以与 Environment 和 PropertySource 交互。它可以是本地属性或系统属性或环境变量。

    PlaceholderConfigurerSupport 的特殊化,可针对当前的 Spring Environment 及其 PropertySources 集解析 bean 定义属性值和 @Value 注释中的 ${...} 占位符。

    【讨论】:

      【解决方案2】:

      PropertyPlaceholderConfigurer

      当我们需要加载一些属性文件,其属性在spring的applicationcontext.xml中使用时,我们使用PropertyPlaceholderConfigurer。我们可以直接使用 JSTL 表达式来使用属性。

      @PropertySource

      此注解将属性贡献给活动的Spring environment。

      上下文:属性占位符

      注册一个组件,该组件可以解析 bean 定义中形式为 ${propertyname} 的占位符。它足够聪明,知道它不仅需要查看通过其 location 属性注册的属性,还需要查看当前活动环境中可用的属性(哪些属性已通过 @PropertySource 注释添加)。

      【讨论】:

      • 感谢 Amogh 的回答,但我怀疑哪个组件(bean)将通过上下文注册:property-placeholder
      • @sandy <context:property-placeholder/> 是等同于 PropertyPlaceholderConfigurer 的 XML
      • 表示通过 注册的 bean 是 PropertyPlaceholderConfigurer 从 3.1 PropertySourcesPlaceHolderConfigure 是 PropertyPlaceHolderConfigure 的替换所以是两者有什么区别。
      • 我不这么认为。 context:property-placeholder 只是基于 xml 的解决方案。
      • 谢谢@Amogh。@value 在propertyplaceholderconfigure 和PropertSourcesplaceholderConfigure 中的注释角色怎么样。
      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 2012-08-06
      • 2014-09-21
      • 1970-01-01
      • 2011-10-13
      相关资源
      最近更新 更多