【问题标题】:How to inject complete propertiesfile in a springbean如何在spring bean中注入完整的属性文件
【发布时间】:2011-08-21 18:42:05
【问题描述】:

我有一个包含很多值的属性文件,我不想在我的 bean 配置文件中单独列出它们。例如:

<property name="foo">
    <value>${foo}</value>
</property>
<property name="bar">
    <value>${bar}</value>
</property>

等等。

我想完全注入 java.util.Properties 或更少 java.util.Map。 有办法吗?

【问题讨论】:

    标签: spring resources properties-file


    【解决方案1】:

    对于 Java 配置,您可以使用如下内容:

    @Autowired @Qualifier("myProperties")
    private Properties myProps;
    
    @Bean(name="myProperties")
    public Properties getMyProperties() throws IOException {
        return PropertiesLoaderUtils.loadProperties(
            new ClassPathResource("/myProperties.properties"));
    }
    

    如果您为每个实例分配一个唯一的 bean 名称 (Qualifier),您也可以通过这种方式拥有多个属性。

    【讨论】:

      【解决方案2】:

      是的,您可以使用&lt;util:properties&gt; 加载属性文件并将生成的java.util.Properties 对象声明为bean。然后,您可以像注入任何其他 bean 属性一样注入它。

      参见section C.2.2.3 of the Spring manual,以及他们的例子:

      <util:properties id="myProps" location="classpath:com/foo/jdbc-production.properties"
      

      记得按照these instructions声明util:命名空间。

      【讨论】:

      • 对待就像其他豆子一样。使用 ref=myProps 在 xml 配置中使用它或 Autowire a Properties 让 Spring 为您处理它。
      【解决方案3】:

      对于 Java 配置,使用 PropertiesFactoryBean:

      @Bean
      public Properties myProperties() {
          PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
          propertiesFactoryBean.setLocation(new ClassPathResource("/myProperties.properties"));
          Properties properties = null;
          try {
              propertiesFactoryBean.afterPropertiesSet();
              properties = propertiesFactoryBean.getObject();
      
          } catch (IOException e) {
              log.warn("Cannot load properties file.");
          }
          return properties;
      }
      

      然后,设置属性对象:

      @Bean
      public AnotherBean myBean() {
          AnotherBean myBean = new AnotherBean();
          ...
      
          myBean.setProperties(myProperties());
      
          ...
      }
      

      希望这对那些对 Java Config 方式感兴趣的人有所帮助。

      【讨论】:

      • 哇,这么多代码。难道没有像 1-liner XML config &lt;util:properties ...&gt; 这样更简单的方法吗?
      • @rustyx 哦!我当时没有找到任何其他方法,但yours 非常好! :)
      【解决方案4】:

      PropertyOverrideConfigurer 机制可以做到这一点:

      <context:property-override location="classpath:override.properties"/>
      

      属性文件:

      beanname1.foo=foovalue
      beanname2.bar.baz=bazvalue
      

      机制在3.8.2.2 Example: the PropertyOverrideConfigurer一节中解释

      【讨论】:

      • 感谢您的回答 - 我的错,属性文件将由“非开发人员”编辑,并且对于没有特殊背景的编辑器来说应该是“易于阅读”的。因此,bean 的名称不是用作键的正确方法。
      【解决方案5】:

      这是@skaffman 在这个 SO 问题中的回应的回应。当我将来尝试解决这个问题时,我会添加更多细节来帮助他人和我自己。

      注入属性文件的三种方式

      方法一

      <bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
          <property name="locations">
              <list>
                  <value>classpath:com/foo/jdbc-production.properties</value>
              </list>
          </property>
      </bean>
      

      参考(link

      方法二

      <?xml version="1.0" encoding="UTF-8"?>
      <beans
          ...
          xmlns:util="http://www.springframework.org/schema/util"
          xsi:schemaLocation="...
          ...
          http://www.springframework.org/schema/util/spring-util.xsd"/>
          <util:properties id="myProps" location="classpath:com/foo/jdbc-production.properties"/>
      

      参考(link

      方法3

      <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="location" value="classpath:com/foo/jdbc-production.properties" />
      </bean>
      

      参考(link

      本质上,所有方法都可以从属性文件中创建一个Properties bean。你甚至可以使用@Valueinjector 直接从属性文件中注入一个值

      @Value("#{myProps[myPropName]}")
      private String myField; 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-09
        • 2012-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 2012-08-06
        相关资源
        最近更新 更多