Spring MVC中的PropertyPlaceholderConfigurer

时间:2014-01-17 09:25:56

标签: java spring spring-mvc

我在项目中使用Spring MVC,我在web.xml中定义了这样的细节:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/applicationContext.xml,
            classpath*:/applicationContext-security.xml
        </param-value>
    </context-param>

<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:/applicationContext-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

我还定义了一个外部属性文件,我想使用属性文件中的详细信息,所以我在PropertyPlaceholderConfigurer中定义了一个applicationContext.xml bean,如下所示:

<context:property-placeholder location="classpath*:/system.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />

但是当我运行我的项目时,我发现我无法使用applicationContext-mvc.xml中的属性文件详细信息,当时我也放了

<context:property-placeholder location="classpath*:/system.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />

在我的applicationContext-mvc.xml中,它运行良好。也就是说我必须在所有Spring配置xml文件中定义PropertyPlaceholderConfigurer bean,它是对的吗?

我不明白,ContextLoaderListener创建了根Web应用程序上下文,DispatcherServlet创建了servlet特定的Web应用程序上下文,

Spring MVC中的层次结构如下:

enter image description here

它没有说我可以在Spring MVC配置文件中使用来自root web应用程序上下文的bean吗?

我不知道为什么,我是否必须在所有Spring配置文件中定义PropertyPlaceholderConfigurer

0 个答案:

没有答案