【问题标题】:spring properties injection without using @Configuration bean不使用@Configuration bean的弹簧属性注入
【发布时间】:2017-01-21 06:40:13
【问题描述】:

我想将一个属性文件加载到一个 spring bean 中。 我找到了两种方法:

  1. 在 xml 中配置为 <util.properties> 并使用 @Resource@Autowired 注入
  2. 将@PropertySource 与@Configuration bean 一起使用。

我不想为这种情况创建 @configuraiton bean,因为所有 spring 配置都在 xml 中维护。

如果我遵循第一种方法 - 我没有以下选项来处理丢失属性文件的情况 @PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)

我希望注入整个属性文件,因为这是查找文件,并且将根据逻辑访问不同的键。 (因此上下文属性占位符将无济于事)

有什么方法可以将属性文件注入 bean 并处理丢失文件的情况?

【问题讨论】:

    标签: spring spring-properties


    【解决方案1】:

    我们可以像下面这样实现它

    <bean id="myLookUp" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
       <property name="ignoreResourceNotFound"><value>true</value></property>
       <property name="locations">
          <list>
            <value>classpath:myLookUp.properties</value>
          </list>
       </property>
    </bean> 
    

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 2012-08-06
      相关资源
      最近更新 更多