Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

spring - PropertyPlaceholderConfigurer: Use external properties file

How to configure PropertyPlaceholderConfigurer to use properties files relative (some directories up) to the war?

We have running a war multiple times and each war should read its configuration for example from ../../etc/db.properties.

Update:

Yes, the properties files are outside the war. The directory structure is:

/htdocs/shop/live/apache-tomat/webapps/shop.war should read /htdocs/shop/live/etc/db.properties

and

/htdocs/shop/test/apache-tomat/webapps/shop.war should read /htdocs/shop/test/etc/db.properties

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally, we have introduced a new resource type "relative:":

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:db.properties</value>
            <value>relative:../../../etc/db.properties</value>
        </list>
    </property>
</bean>

We have extended XmlWebApplicationContext to inject custom resource handling:

public class Context extends XmlWebApplicationContext {
    @Override
    public Resource getResource(String location) {
        if (location.startsWith(RelativeResource.RELATIVE_URL_PREFIX)) {
            String relativePath = location.substring(RelativeResource.RELATIVE_URL_PREFIX.length());
            return new RelativeResource(getServletContext(), relativePath);
        }
        return super.getResource(location);
    }
}

Here is the relative resource class:

public class RelativeResource extends AbstractResource {
    public static final String RELATIVE_URL_PREFIX = "relative:";

    private final ServletContext servletContext;
    private final String relativePath;

    public RelativeResource(ServletContext servletContext, String relativePath) {
        this.servletContext = servletContext;
        this.relativePath = relativePath;
    }

    @Override
    public String getDescription() {
        return "RelativeResource [" + relativePath + "]";
    }

    @Override
    public boolean isReadable() {
        return true;
    }

    @Override
    public boolean isOpen() {
        return true;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        String rootPath = WebUtils.getRealPath(servletContext, "/");
        if (!rootPath.endsWith(File.separator)) rootPath += File.separator;
        String path = rootPath + relativePath;
        return new FileInputStream(path);
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...