【问题标题】:Load a property file when context is loading giving the file path as a property of another property file加载上下文时加载属性文件,将文件路径作为另一个属性文件的属性
【发布时间】:2014-04-16 02:49:15
【问题描述】:

我遇到需要在系统中使用两个属性文件的情况。

  1. 系统属性

    key.supportiveFile=C:\keypath\supportive.properties 
    

    这个key path可以在classpath之外。我想为 Supportive.properties 作为 System.properties 中的键。 support.properties 文件有

  2. supportive.properties。

    Supportive.properties 具有键 key1 和 key2。 supportive.properties 文件中的属性必须与上下文一起加载

理想情况下,我的属性文件将如下所示。

supportive.properties
key1=value1
key2=value2

在 spring.xml 中,我尝试加载 support.properties 并注入 spring.xml 中定义的 key1 和 key2 值,如下所示。

我。加载属性文件

 <context:property-placeholder location="classpath:System.properties,file:${key.supportiveFile}"/>

二。这是一个示例 bean,我想如何将我的密钥注入 bean。

    <bean id="helloWorldBean" class="com.sample.snippets.enterprise.services.HelloWorld">
         <property name="prefixProp" value="${key1}" />
         <property name="suffixProp" value="${key2}" /> 
    </bean>  

服务器日志显示 Could not resolve placeholder 'key.supportiveFile' in [file:${key.supportiveFile}],但它失败并且没有加载 support.property 文件。

请告知如何从技术上解决此问题。

【问题讨论】:

  • 请使用正确的文本格式!

标签: java spring


【解决方案1】:

你可以这样做:

<context:component-scan base-package="com.foo.bar.config"/>

<context:property-placeholder location="file:${key.supportiveFile}" />

<bean id="helloWorldBean" class="com.sample.snippets.enterprise.services.HelloWorld">
    <property name="prefixProp" value="${key1}" />
    <property name="suffixProp" value="${key2}" /> 
</bean>

com.foo.bar.config 中你有以下类:

package com.foo.bar.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:/System.properties")
public class SpringConfig{

}

这种方法的解释来自this JIRA issue

【讨论】:

  • 谢谢我尝试了你的方法,但不幸的是现在加载文件时出现以下错误:${key.supportiveFile} resource.stackoverflow.com/questions/10627856/…
  • 您到底尝试了什么?向我们展示您的代码和堆栈跟踪!
  • 抱歉,这是 Windows 路径问题。这适用于 Spring3.x。我想将实际实现应用于没有 org.springframework.context.annotation.Configuration 类的 Spring 版本 2.5.1 的项目。在这种情况下有没有办法做。 ?
  • 是否可以只使用
  • 我不这么认为。我知道我前段时间尝试使用两个具有不同后缀(“$”和“%”)的属性占位符,一个引用另一个占位符。无法让它工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 2013-04-26
  • 2019-03-04
  • 1970-01-01
  • 2013-01-15
  • 2015-12-15
相关资源
最近更新 更多