简体   繁体   中英

Spring MVC - Is it possible for a jar file to refer to a properties file that is not in the jar file?

I have a spring MVC web application that has the following structure:

myapp
    |-META-INF
    |-WEB-INF
        |-classes
        |   |-conf
                |-application.properties
        |-lib
        |   |-externalApp.jar
        |       |-conf
        |           |-applicationContext.xml
        |
        |-applicationContext.xml
        |-myapp-servlet.xml

In myapp/WEB-INF/applicationContext , i imported the applicationContext.xml file that is in the jar file as shown below:

<import resource="classpath:WEB-INF/conf/applicationContext.xml" /> 

The beans in the imported resource work fine and i can see them in my web application's controller/service classes.

The problem i have is that the context file in the jar file (ie WEB-INF/lib/externalApp.jar/applicationContext.xml ) has configuration for loading a properties file. The properties have to be set by the web application so the properties file is in the webapp. The configuration in the jar file's context file looks like this:

I want the above property to load the property file that is in the web application so i set its value to be as shown below:

<bean class="com.myapp.ExternalAppPropertyPlaceholderConfigurer">
        <property name="location" value="classpath:conf/application.properties" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

With the above setting, the classes in the jar file that expect these properties to be availbe still cant get access to the properties.

The question i guess is how can i get the properties file that is in WEB-INF/classes/conf/application.properties to be accessible to the objects in the jar file that is located in WEB-INF/lib/externalApp.jar .

Looking at the stack traces i am getting, it looks as though the objects referred in the imported context file are loaded first before the properties are loaded which is not i want.

Thanks.

You can use the classpath*: prefix like this

<bean class="com.myapp.ExternalAppPropertyPlaceholderConfigurer">
    <property name="location" value="classpath*:conf/application.properties" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

EDIT

Since your conf/application.properties is in your web app, you must define it in your web-app applicationContext (not in the jar as you do now). And define it before importing the applicationContext of your jar. ie put something like this in your web-app applicationContext:

<bean class="com.myapp.ExternalAppPropertyPlaceholderConfigurer">
    <property name="location" value="classpath:conf/application.properties" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<import resource="classpath*:/conf/applicationContext.xml" />

and remove the declaration of the properties from your jar applicationContext.

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