3

I use Spring MVC in my project, I define the details in web.xml like this :

<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>

I also define a external properties file, I want use the details in the properties file, so I define a PropertyPlaceholderConfigurer bean in applicationContext.xml like this :

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

but when i run my project, I find that I can't use the properties file details in applicationContext-mvc.xml , when I also put the

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

in my applicationContext-mvc.xml, it works well。 That is say I must define PropertyPlaceholderConfigurer bean in all Spring configuration xml file, it is right?

I do not understand, the ContextLoaderListener create the root web application context, the DispatcherServlet create the servlet-specifical web application context,

the hierarchy in Spring MVC like this:

enter image description here

it didn't say that I could use the bean extends from root web application context in Spring MVC configuration file?

I don't know why, whether I must define PropertyPlaceholderConfigurer in all Spring configuration file?

Rahul
  • 44,383
  • 11
  • 84
  • 103
Rocky Hu
  • 1,326
  • 4
  • 17
  • 32
  • 1
    The `PropertyPlaceholderConfigurer` is a `BeanFactoryPostProcessor` those operate only on the ApplicationContext they are defined in not on parent nor on child contexts. Each context needs to have a `PropertyPlaceholderConfigurer`. Each context isn't the same as each xml file! – M. Deinum Jan 17 '14 at 09:45
  • M. Deinum, but why define another configuration file application-hibernate.xml, I also didn't defnid PropertyPlaceholderConfigurer bean in this file, I can use ${variable}, why? – Rocky Hu Jan 17 '14 at 09:57
  • As mentioned `ApplicationContext` isn't a single xml file (as I mentioned in my initial comment). `ContextLoaderListener` creates an `ApplicationContext` based on all xml files, the same for `DispatcherServlet`. – M. Deinum Jan 17 '14 at 10:21

0 Answers0