Spring应用程序上下文外部属性

Den*_*Ich 29 spring properties

我有一个Spring应用程序,它到目前为止运行良好.现在我希望属性文件在外部配置文件夹中,而不是在打包的jar中更改内容而无需重新打包.这就是我得到的:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- <property name="locations" value="classpath:/springcontext.properties"/>  -->
<property name="locations" value ="config/springcontext.properties" />
Run Code Online (Sandbox Code Playgroud)

经过评估的一个正在工作,而另一个我没有工作:/有人可以帮忙吗?

编辑:Thx 4评论到目前为止.

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

mas*_*ted 47

你可以尝试这样的事情:

<context:property-placeholder 
        location="${ext.properties.dir:classpath:}/servlet.properties" />
Run Code Online (Sandbox Code Playgroud)

ext.properties.dir在应用程序服务器/ jvm中定义属性,否则将使用默认属性位置"classpath:/"(即.jar或.war的类目录):

-Dext.properties.dir=file:/usr/local/etc/
Run Code Online (Sandbox Code Playgroud)

BTW,非常有用的博文.


fmu*_*car 12

<context:property-placeholder location="classpath*:spring/*.properties" />
Run Code Online (Sandbox Code Playgroud)

如果将它放在类路径中某个名为spring的目录中的某个位置(相应地更改名称/ dirs),则可以使用上面的方法进行访问

<property name="locations" value ="config/springcontext.properties" />
Run Code Online (Sandbox Code Playgroud)

这将指向web-inf/classes/config/springcontext.properties


Pra*_*nti 12

您可以使用文件前缀来加载外部应用程序上下文文件

  <context:property-placeholder location="file:///C:/Applications/external/external.properties"/>
Run Code Online (Sandbox Code Playgroud)


Sri*_*har 10

这个博客可以帮到你.诀窍是使用SpEL(spring表达式语言)来读取系统属性,比如user.home使用SpEL读取用户主目录,可以
#{ systemProperties['user.home']}在bean元素中使用表达式.例如,要访问存储在主目录中的属性文件,可以在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>
Run Code Online (Sandbox Code Playgroud)


Aru*_*ran 6

这个问题有点陈旧,但想分享一些对我有用的东西.希望它对那些正在搜索访问外部位置属性的信息的人有用.

这对我有用.

  1. 属性文件内容:

    PROVIDER_URL=t3://localhost:8003,localhost:8004
    
    Run Code Online (Sandbox Code Playgroud)
  2. applicationContext.xml 文件内容:(Spring 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>
    
    Run Code Online (Sandbox Code Playgroud)

${PROVIDER_URL} 被替换为文件属性中的值