2015-06-19 25 views
1

我已經從戰爭外部化了配置文件,我想知道如何解決我被迫在應用上下文中提供的絕對位置以使其工作。外部化應用上下文和戰爭hibernate.properties

在我的web.xml我有這個 -

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>file:C:/Program%20Files/somedirectory/webappContext.xml</param-value> 
</context-param> 

C:/Program%20Files/somedirectory/webappContext.xml具有如下條目:

<import resource="applicationContext-persistence.xml" /> 

應用程序上下文 - 持久性。 xml和webAppContext.xml存在於同一個目錄下,它有一行 -

<util:properties id="hibernateProperties" location="hibernate.properties" /> 
<encryption:encryptable-property-placeholder encryptor="stringEncryptor" location="hibernate.properties"/> 

Tomcat is unab以找到位於同一目錄中的hibernate.properties文件。相反,它迫使我將其定義如下:

<util:properties id="hibernateProperties" location="file:C:/Program%20Files/somedirectory/hibernate.properties" /> 
<encryption:encryptable-property-placeholder encryptor="stringEncryptor" location="file:C:/Program%20Files/somedirectory/hibernate.properties"/> 

有沒有辦法避開指定完整路徑?爲什麼webAppContext.xml中的行可行,而不是applicationcontext-persistence.xml中的行?兩者在性質上與引用位於同一目錄中的文件類似。

+0

儘管他們可能出現的是類似的,他們都沒有。你可以嘗試用'。/'加前綴,但我懷疑這是否有效。 –

+0

試過。不起作用。 'file:./','./'無效。和絕對'classpath:hibernate.properties'將無法正常工作,因爲程序文件/ somedirectory不在tomcat容器的類路徑中 – sethu

回答

1

webappContext.xml應該駐留在戰爭,但它可以包含類似:當應用程序啓動

<bean id="coreProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="locations"> 
     <list> 
      <value>classpath:/properties/*.properties</value> 
      <value>file:${app.home}/properties/*.properties</value> 
     </list> 
    </property> 
</bean> 

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> 
    <property name="ignoreResourceNotFound" value="false"/> 

    <property name="properties" ref="coreProperties" /> 
</bean> 

現在,你只需要通過app.home系統屬性:

# cat setenv.sh 
export CATALINA_OPTS="$CATALINA_OPTS -Dapp.home=/home/user/my-app" 
1

你是試圖欺騙框架,它不想幫助你做到這一點。

更嚴重的是,這是資源在應用程序環境中解決的方式。從doc提取Spring框架的當前版本:

Prefix  Example      Explanation 
classpath: classpath:com/myapp/config.xml Loaded from the classpath. 
file:  file:///data/config.xml  Loaded as a URL, from the filesystem 
http:  http://myserver/logo.png  Loaded as a URL. 
(none)  /data/config.xml    Depends on the underlying ApplicationContext. 

當你正在使用的WebApplicationContext(正常的網絡應用程序...),不合格的資源將應用程序的根目錄下進行搜索。

但是,這還不是全部,同一文檔後說:

FileSystemApplicationContext會簡單地讓所有綁定的FileSystemResource實例把絕對路徑,相對的,他們是否開始與一家領先的斜線與否。在實踐中,這意味着以下是等價的:

ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/context.xml"); 
ApplicationContext ctx = new FileSystemXmlApplicationContext("/conf/context.xml"); 

這意味着,無需使用自定義的ResourceLoader不能使用相對路徑,並有儘快使用file:full_path_to_file爲你的資源是不是戰爭裏面。

無論如何,你肯定不會真的想這麼做:你的戰爭現在不能被部署在任何其他系統,你自己的服務器!確實有可能在戰爭之外有(小)部分配置,它有一定的意義,因爲某些元素只有在部署時才能知道。有兩種攜帶方式來做到這一點:

  • 使用環境變量,因爲它們可以用在Spring配置文件來解決佔位符:這主要是用於簡單的配置數據:網站管理員的電子郵件,郵件服務器,網址爲後端,或者針對不同profiles
  • 使用servlet容器的配置和JNDI之間進行動態選擇:這是經常使用的數據庫配置