【问题标题】:Tomcat Context-Params ignored in Spring webapp when using PropertyPlaceholder使用 PropertyPlaceholder 时在 Spring webapp 中忽略 Tomcat 上下文参数
【发布时间】:2013-07-05 15:33:14
【问题描述】:

我以前使用现在已弃用的类org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer 从服务器的文件系统加载属性文件。我定义了以下 bean:

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="locations" value="${config}"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="searchContextAttributes" value="true"/>
    <property name="contextOverride" value="false"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="searchSystemEnvironment" value="false"/>
</bean>

config 是启动 Tomcat 时传递的参数,即

-Dconfig=/path/to/application.properties

对于 webapp,我还有一个上下文文件:

<Context docBase="/path/to/application.war">
    <Parameter name="host" value="localhost" override="false"/>
    <Parameter name="port" value="8080" override="false"/>
</Context>

如果.properties 文件(由-Dconfig 参数指定)包含一些其他bean 引用的属性,则使用.properties 文件中的值,否则使用上下文xml 文件中的值。

这允许我使用 WAR 部署一组默认属性,如果需要,我可以指定一个 .properties 文件来覆盖特定值。

现在,我正在更新以使用 Spring 3.1 中的新属性抽象,但我似乎无法弄清楚与之等效的方法是什么?

我以相同的方式部署了相同的上下文文件和战争,现在我在应用程序中有以下内容:

<context:property-placeholder
        location="${config}"
        system-properties-mode="OVERRIDE"
        ignore-resource-not-found="true"
        ignore-unresolvable="true"/>

这会查找并使用属性文件中的属性,但它不使用上下文 XML 文件中的值。

如何让我的应用程序在使用这个新的属性占位符时使用上下文参数?

谢谢。

【问题讨论】:

    标签: spring


    【解决方案1】:

    总结问题是,当使用 Spring 3.1 中引入的新 Property Placeholder 命名空间时,servlet 上下文文件中的上下文参数没有用于解析占位符。

    我想出了一个解决办法,如下

    <context:property-placeholder location="${config}" local-override="true" ignore-resource-not-found="true"/>
    

    我可以使用 JVM 参数在本地文件系统上指定一个或多个 *.properties 文件,例如:

    -Dconfig=/path/app.properties
    

    如果在检查 app.properties 文件后无法解析占位符属性,则检查 Servlet 上下文参数。

    这允许我使用 web.xml 中的上下文参数来获得默认值,并且我可以在需要时通过使用 config JVM arg 指定 *.properties 文件的位置来覆盖这些值。

    让它以这种方式工作的关键是包含local-override="true",默认情况下为false。我不完全确定它是否有意义,因为该属性的描述是:

    指定本地属性是否覆盖文件中的属性。默认 为“假”:文件中的属性会覆盖本地默认值。

    如果app.propertiesweb.xml 中存在相同的属性键,则使用app.properties 中的值。

    【讨论】:

      【解决方案2】:

      除非定义了基于用户的属性文件,否则 Spring 使用默认属性文件。如果你想控制 .properties 文件,请按照here 发布的说明进行操作。

      如果您想利用 application.properties,有两种方法可以做到这一点。

      <!-- allows for ${} replacement in the spring xml configuration from the 
          system.properties file on the classpath -->
      <util:properties id="appProperties" location="classpath:application.properties"/>
      <context:property-placeholder location="classpath:application.properties"/>
      

      util 标签允许您使用 Property 类来读取整个应用程序的属性。例如:

      @Autowired
      public MyPropertyReader(Properties appProperties) {
          String prop1 =  appProperties.getProperty("my.address");
          String prop2 =  appProperties.getProperty("my.version");
      }
      

      如果您想使用上下文文件中的值,请使用 context:property-placeholder 标记。然后你可以使用你的价值观作为

      <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="${jms.primary.server}"/>
      

      例如 jms.primary.server=172.168.10.18:6161 在 application.properties 中的位置。

      【讨论】:

      • 我认为您没有完全理解我的原始帖子。我已经使用属性文件解析了占位符。该问题询问有关使用上下文参数的问题。
      • 您是否点击了第一句中的链接?剩下的答案只是为使用 .properties 和 application.properties 的其他开发人员提供的附加组件。
      • 问题在于上下文参数 - 而不是属性文件。
      猜你喜欢
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-12
      • 2022-01-25
      • 2017-11-28
      • 2016-04-14
      • 1970-01-01
      相关资源
      最近更新 更多