简体   繁体   中英

Difference Between web-app's context-param and servlet's init-param?

I'm using Spring MVC. In a Controller class, I want to use the @Value annotation to inject a value that comes from a properties file:

@Value("${upload.dir}")
private String uploadDir;

So I need to put a property-placeholder somewhere.

The web.xml is typical:

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
    ...
</servlet>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/business-context.xml</param-value>
</context-param>

First, I put the placeholder in business-context.xml. It doesn't work: "Could not autowire field".

Then I put it in mvc-dispatcher-servlet.xml, it works.

So I'm confused about these two contexts, are they the same one or different? Because the beans I defined in business-content.xml can be autowired, but the @Value doesn't work.

I don't want to put the placeholder in both xml files 'cause I have a long 'location' property. Also the business-context.xml will be used by some jobs, so it cannot be omitted.

Any way to make placeholder defined in business-context.xml become visible in mvc-dispatcher-servlet.xml as well?

A BeanFactoryPostProcessor which is what the property-placeholder is will only operate (and be visible) to the application context it is defined in. This is by design. So no you cannot make a property-placeholder from a parent visible to a child context (well with some nasty hacks you could).

As a work around you could do the following in your business-context.xml

<util:properties id="applicationProperties" location="path-to-your-very-long-location" />
<context:property-placeholder properties-ref="applicationProperties" />

and this in your mvc-dispatcher-servlet.xml.

<context:property-placeholder properties-ref="applicationProperties" />

Define the same <context:property-placeholder ../> in both xml context and simply reference the already loaded properties. Added advantage the properties are only loaded once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM