context:property-placeholder不起作用

时间:2013-01-29 09:16:37

标签: spring java-ee

我的任务是相对简单的尝试,但我在这一点上陷入困​​境。我的问题是

这两种语法是否相同?

A

<context:property-placeholder location="classpath:application.properties" system-properties-mode="OVERRIDE"/> 

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:application.properties"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>

AppContext

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd"
>

结果就在这里

 https://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

2 个答案:

答案 0 :(得分:2)

在Spring 3.1的版本中(在xml架构中引用)它们并不完全相同

context:property-placeholder将定义PropertySourcesPlaceholderConfigurer的实例 虽然你已经用bean声明声明了一个PropertyPlaceHolderConfigurer实例。

这就是为什么有时候使用标签代替直接bean声明是合理的(即定义完成了,而不是如何完成)。

来自PropertySourcesPlaceholderConfigurer javadoc:

  

从Spring 3.1开始,应该使用PropertySourcesPlaceholderConfigurer   优先于此实施;它更灵活   利用Environment和PropertySource机制也是如此   在Spring 3.1中提供。

     

在Spring 3.1之前,命名空间   element注册了PropertyPlaceholderConfigurer的一个实例。它   如果使用spring-context-3.0.xsd定义,它仍然会这样做   命名空间。也就是说,你可以保留注册   PropertyPlaceholderConfigurer通过命名空间,即使使用   春季3.1;只是不要更新您的xsi:schemaLocation并继续   使用3.0 XSD。

答案 1 :(得分:0)

是的,他们是一样的。

根据context namespace documentation

  

“OVERRIDE”表示应首先针对系统​​属性解析占位符,然后针对任何本地属性

解析占位符

根据PropertyPlaceholderConfigurer

  

在尝试指定的属性之前,首先检查系统属性。这允许系统属性覆盖任何其他属性源。