2011-11-25 49 views
23

我有一个Spring应用程序,它的运行良好。现在我想要外部配置文件夹中的属性文件,而不是在打包的jar文件中进行更改,而不需要重新打包。这是我得到了什么:Spring应用程序上下文外部属性?

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<!-- <property name="locations" value="classpath:/springcontext.properties"/> --> 
<property name="locations" value ="config/springcontext.properties" /> 

的outcommented一个正在工作,另一个我不上班:/有人能帮忙吗?

编辑: Thx目前为止4评论。

也许我的问题不够明确:)。我执行一个Maven构建,一切都将被打包,并且我希望此文件夹不在outcomming jar旁边的包装螺母中,并且在此文件夹中我想要属性文件。可能?

+0

尝试用奇异'location',虽然'locations'应该管用。也可以尝试从类路径引用中删除前导'/'。 –

+0

任何人都可以帮助解决新的问题? –

+0

@DennisIch你是如何解决这个问题的?我正面临类似的问题 – happybuddha

回答

11
<context:property-placeholder location="classpath*:spring/*.properties" /> 

如果你的地方放置在一个名为弹簧类路径(更改名称/相应迪尔斯),你可以用上面

<property name="locations" value ="config/springcontext.properties" /> 

这将指向WEB-INF/classes下访问/config/springcontext.properties

+0

它不是一个Web应用程序,它与一些命令行功能是独立的,为此我需要它在jar外。 –

+0

那么它就在你的jar文件的旁边,如果它不是一个web项目 – fmucar

2

一种方法是将你的外部配置文件夹添加到java进程的类路径中。这就是我过去经常这样做的方式。

+0

看来Spring的'财产placeholder'不能够在那里装载应用程序上下文定义包外财产的文件(见http://forum.springsource.org/showthread.php?87994-Classpath*-in-上下文属性占位符-犯规-拾取系统类路径&p = 295698#post295698)。你是如何实现加载外部属性文件的? –

+0

您是否尝试将其添加到清单文件? – pap

+1

嗯...将什么添加到清单文件? MANIFEST.MF文件驻留在一个包中,它如何帮助? –

11

您可以使用文件前缀来加载外部应用程序上下文文件中的一些这样的事

<context:property-placeholder location="file:///C:/Applications/external/external.properties"/> 
+0

也可以作为'PropertyPlaceholderConfigurer'的'location'属性 –

43

你可以尝试这样的事情:

<context:property-placeholder 
     location="${ext.properties.dir:classpath:}/servlet.properties" /> 

而在你的应用服务器/ JVM定义ext.properties.dir财产,否则默认属性位置“类路径:/”(名.jar即类目录或的.war)将被使用:

-Dext.properties.dir=file:/usr/local/etc/ 

顺便说一句,非常有用blog post

9

This博客可以帮助你。诀窍是使用SpEL(Spring表达式语言)来读取像user.home这样的系统属性,使用SpEL读取用户主目录,您可以在您的bean元素内部使用
#{ systemProperties['user.home']}表达式。例如,要访问存储在主目录中的属性文件,您可以在PropertyPlaceholderConfigurer中使用以下内容,它对我很有用。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <value>file:#{ systemProperties['user.home']}/ur_folder/settings.properties</value> 
    </property> 
</bean> 
5

这个问题有点古怪,但想分享一些对我有用的东西。希望对于在外部位置搜索某些信息访问属性的用户有用。

这是为我工作。

  1. 属性文件内容:

    PROVIDER_URL=t3://localhost:8003,localhost:8004 
    
  2. applicationContext.xml文件的内容:(春季3.2。3)

    注:${user.home}是从OS的系统性能。

    <context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/> 
    
    <bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
        <property name="environment"> 
         <props> 
          <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> 
          <prop key="java.naming.provider.url">${PROVIDER_URL}</prop> 
         </props> 
        </property> 
    </bean> 
    

${PROVIDER_URL}得到的属性与价值替换的文件

1
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" /> 

这对我的作品。 本地开发机器路径是C:\ APPS \ tomcat的\ ATH \ ath_conf和服务器/应用/ tomcat的/ ATH/ath_conf

这两部作品对我来说

相关问题