【问题标题】:Spring application context external properties?Spring应用程序上下文外部属性?
【发布时间】:2012-01-06 09:41:27
【问题描述】:

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

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

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

编辑: 到目前为止,谢谢 4 cmets。

也许我的问题不够清楚:)。我执行 Maven 构建,所有内容都将被打包,我希望这个文件夹不在输出 jar 旁边的包螺母中,在这个文件夹中我想要属性文件。可能吗?

【问题讨论】:

  • 尝试使用单数 location,尽管 locations 应该可以。还可以尝试从类路径引用中删除前导 /
  • 有人可以帮忙解答这个新问题吗?
  • @DennisIch 你是怎么解决这个问题的?我面临着类似的问题
  • 只需将其复制到应用程序文件夹即可;)。我使用 mvn “ant task”进行复制作业。

标签: spring properties


【解决方案1】:
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" />

这对我有用。 本地开发机器路径为 C:\apps\tomcat\ath\ath_conf 并在服务器 /apps/tomcat/ath/ath_conf

两者都适合我

【讨论】:

    【解决方案2】:

    This博客可以帮到你。诀窍是使用 SpEL(弹簧表达式语言)读取系统属性,如 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>
    

    【讨论】:

    • 感谢我的帮助
    【解决方案3】:

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

    这对我有用。

    1. 属性文件内容:

      PROVIDER_URL=t3://localhost:8003,localhost:8004
      
    2. applicationContext.xml 文件内容:(Spring 3.2.3)

      注意:${user.home} 是操作系统的系统属性。

      <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} 被文件属性中的值替换

    【讨论】:

      【解决方案4】:

      你可以试试这样的:

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

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

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

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

        • 也可以作为locationPropertyPlaceholderConfigurer的属性使用
        【解决方案6】:
        <context:property-placeholder location="classpath*:spring/*.properties" />
        

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

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

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

        【讨论】:

        • 它不是一个 Web 应用程序,它是一个具有一些命令行功能的独立应用程序,为此我需要它在 jar 之外。
        • 如果不是 web 项目,那么它就在您的 jar 文件旁边
        【解决方案7】:

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

        【讨论】:

        • 似乎 Spring 的 property-placeholder 无法在定义应用程序上下文的包之外加载属性文件(请参阅 forum.springsource.org/showthread.php?87994-Classpath*-in-context-property-placeholder-doesnt-pick -system-classpath&p=295698#post295698)。您是如何实现加载外部属性文件的?
        • 嗯...在清单文件中添加什么? MANIFEST.MF 文件位于一个包中,那么它有什么帮助呢?
        猜你喜欢
        • 1970-01-01
        • 2020-09-25
        • 1970-01-01
        • 1970-01-01
        • 2015-04-03
        • 2022-01-22
        • 2020-06-08
        • 1970-01-01
        • 2015-01-23
        相关资源
        最近更新 更多