2013-07-03 102 views
2

如何配置PropertyPlaceholderConfigurer以使用與戰爭相關的屬性文件(某些目錄)?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

+0

所以屬性文件是戰爭之外? –

+1

如果你發佈你的目錄結構,我可以給你一個更好的答案。 –

+0

添加了一些信息。 – mazatwork

回答

0

在您的配置,您可以指定從classpath的屬性,而不是相對於配置文件。

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

爲了達到此目的,您必須確保屬性文件使其進入類路徑。

2

最後,我們引入了一個新的資源類型 「相對:」:

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

} 
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上下文,不堅持這個砍死上下文實現,你可以使用來自Spring發行版的任何(例如,不是XmlWebApplicationContext,但是ClassPathXmlApplicationContext)。

0

不知何故,我無法獲得所需的路徑跟隨他人的方法,所以這裏是我的工作版本,主要基於德米特里的答案(在xml中的用法是相同的),而isReadable()和getInputStream()看起來更多像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() { 
     return true; 
    } 

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

    @Override 
    public InputStream getInputStream() throws IOException { 
     String rootPath = this.getClass().getResource("/").getPath(); 
     rootPath = URLDecoder.decode(rootPath, "UTF-8"); 
     if (!rootPath.endsWith(File.separator)) rootPath += File.separator; 
     String path = rootPath + relativePath; 
     return new FileInputStream(path); 
    } 
} 
相關問題