3

戦争に関連するプロパティファイル(いくつかのディレクトリまで)を使用するようにPropertyPlaceholderConfigurerを構成する方法は?

戦争を複数回実行しており、各戦争は、たとえば からその構成を読み取る必要があります../../etc/db.properties

アップデート:

はい、プロパティファイルは戦争の外にあります。ディレクトリ構造は次のとおりです。

/htdocs/shop/live/apache-tomat/webapps/shop.war 読むべき /htdocs/shop/live/etc/db.properties

/htdocs/shop/test/apache-tomat/webapps/shop.war 読むべき /htdocs/shop/test/etc/db.properties

4

4 に答える 4

2

最後に、新しいリソース タイプ「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>

カスタム リソース処理を挿入するために、XmlWebApplicationContext を拡張しました。

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);
    }
}

相対リソース クラスは次のとおりです。

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);
    }

}
于 2013-07-04T16:13:29.020 に答える
1

mazatwork ソリューションに基づく私のコード:

public class RelativeResource extends AbstractResource {
    private final String relativePath;

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

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

    @Override
    public boolean isReadable() {
        File resourceFile = new File(getAbsoluteFileLocation());
        return resourceFile.exists();
    }

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

    @Override
    public InputStream getInputStream() throws IOException {
        return new FileInputStream(getAbsoluteFileLocation());
    }

    private String getAbsoluteFileLocation() {
        return Paths.get("").toAbsolutePath().resolve(relativePath).toString();
    }
}

その後、たとえばxmlで書くことができます:

<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value type="com.blabla.RelativeResource">overrideProperties.properties</value>
        </list>
    </property>
    <property name="ignoreResourceNotFound" value="true"/>
</bean>

この方法の長所 - Spring Context をハックせず、このハッキングされたコンテキスト実装に固執しないでください。Spring ディストリビューションの任意の (たとえば、XmlWebApplicationContext ではなく ClassPathXmlApplicationContext) を使用できます。

于 2015-10-22T20:34:22.287 に答える
0

classpath構成では、構成ファイルに関連するのではなく、からプロパティを指定できます。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>

これが機能するには、プロパティ ファイルがクラスパスに到達していることを確認する必要があります。

于 2013-07-03T08:56:24.533 に答える