【问题标题】:java.util.Properties: How do I retrieve values from Properties in spring config xmljava.util.Properties:如何从 Spring config xml 中的属性中检索值
【发布时间】:2016-06-06 20:15:19
【问题描述】:

我有一个 java.util.Properties 对象,其中几乎没有键值对。我正在尝试在 spring 配置文件中使用这个 Property 对象,即在 spring 配置中定义键,并且在运行时,它应该从属性对象中获取值。

例如:

<bean id="test" class="com.sample.Test">
    <constructor-arg value="${PROPERTY_KEY} />
</bean>

现在在运行时,构造函数应该获取属性对象中存在的值。

有没有办法做到这一点?

注意:我不想在这里使用 config.properties。希望使用 java.util.Properties

谢谢, 拉胡尔

【问题讨论】:

  • &lt;context:property-placeholder properties-ref="your-properties-bean-id" /&gt;。像这样的东西应该可以解决问题。
  • 感谢 Denium。我会试试这个。但不确定如何,因为我在 xml 中没有属性 bean id,我也无法创建一个。
  • 那么你在哪里有那个属性对象呢?为什么不能在 XML 中创建它?您能否为您的问题添加更多信息。

标签: java spring properties configuration


【解决方案1】:
 <context:property-placeholder location="classpath:placeholder.properties"/>

<bean id="properties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="location" value="classpath:<file-name>.properties" />

</bean>

【讨论】:

  • 他明确表示不想使用文件。
  • 嗨,正如 Denium 所说,我不想使用任何属性文件来检索值。
【解决方案2】:

首先你必须创建 bean 来访问你的属性文件,如下所示

<bean id="appProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="singleton" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
            <list>
          <value>classpath*:localhost-mysql.properties</value>
          <value>classpath*:mail-server.properties</value>
          </list>
    </property>     
</bean>
<bean id="placeholderConfig"    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties" ref="appProperties" />
</bean>

接下来,您可以从属性文件中访问键值对,如下所示

<bean id="mailServerSettings" class="com.ratormonitor.app.context.EmailSettings">
    <property name="host" value="${mail.server.host}" />
    <property name="port" value="${mail.server.port}" />
    <property name="username" value="${mail.server.username}" />
    <property name="password" value="${mail.server.password}" />
    <property name="requestContextPath" value="${request.context.path}" />
</bean>

希望这段代码能解决您的问题。

【讨论】:

  • 嗨 Senthil,感谢您的回答,但是正如问题中提到的,我不想使用任何属性文件。
【解决方案3】:

您可以使用 Spring 表达式语言 (SpEL) 在 spring 配置 xml 文件中获取 java 对象值。

一个例子:

<property name="misfireInstruction"
                          value="#{T(org.quartz.CronTrigger).MISFIRE_INSTRUCTION_FIRE_ONCE_NOW}"/>

【讨论】:

    【解决方案4】:

    所以我就是这样做的:

    正如我所说,我有一个 java.util.Properties 对象。然后我创建了一个扩展 PropertySource>

    的 CustomProperty 类
    public class CustomPropertySource extends PropertySource<Map<String, Object>>
    

    然后在我的主课中,我做了以下事情:

    AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] {springConfigLocation, false);
    context.getEnvironment().getPropertySources.addlast(new CustomPropertySource("custom", propertiesObject));
    conext.refresh();
    

    然后在 spring 配置文件中,我必须添加这个:

    <context: property-placeholder ignore-unresolvable="true"/>
    

    因此,通过这种方式,我可以获取 spring 配置文件中定义的键的值,就像我们从属性文件中获取值一样。

    谢谢, 拉胡尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2011-08-01
      • 2011-01-18
      • 2021-11-22
      • 2013-05-22
      • 1970-01-01
      • 2020-09-27
      相关资源
      最近更新 更多