Tomcat + spring 使用站外的配置文件

来源:互联网 发布:大连知行中学学区 编辑:程序博客网 时间:2024/09/21 09:02

对于配置文件我们原来使用spring的applicationContext.xml来配置


        <bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
  <property name="ignoreResourceNotFound" value="true" />
  <property name="locations">
   <list>
    <!-- standard configuration -->
    <value>classpath*:/application.properties</value>
    <!-- local dev configuration -->
    <value>classpath*:/application.local.properties</value>
    <!-- production server configuration -->

    <value>classpath*:application.server.properties</value>
    <   </list>
  </property>
 </bean>

原来的配置文件是放在src/main/resources/application.properties 下面的, 但在生产/测试服务器中部署的话经常要要改动该配置文件, 先解包然后再改于是客户提出要求把配置文件放到站外.

修改如下

 

value>file:/var/myapp/application.server.properties</value>


但客户要求不能硬编码外部配置文件的路径, 并把这个路径放在tomcat的配置里面. 于是想改成
<value>file:${yourpath}/application.server.properties</value>


这种形式, 但这样要求yourpath为系统变量. 最后的解决方案是在tomcat的<tomcatHome>/conf/context.xml中添加一个环境变量, 然后重写了一个ContextLoaderListener

最后的配置为
<tomcatHome>/conf/context.xml

[xhtml] view plaincopyprint?
  1. <Environment name="yourConfigPath" value="E:/"  
  2.              type="java.lang.String" override="false" />  

 

applicationContext.xml
...

[xhtml] view plaincopyprint?
  1. <bean  
  2.   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.   <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />  
  4.   <property name="ignoreResourceNotFound" value="true" />  
  5.   <property name="locations">  
  6.    <list>  
  7.     <!-- standard configuration -->  
  8.     <value>classpath*:/application.properties</value>  
  9.     <!-- local dev configuration -->  
  10.     <value>classpath*:/application.local.properties</value>  
  11.                                 <!-- production server configuration -->  
  12.     <value>file:${yourConfigPath}/application.properties</value>  
  13.    </list>  
  14.   </property>  
  15.  </bean>  
       

...

 

web.xml


        

[xhtml] view plaincopyprint?
  1. <context-param>  
  2.   <param-name>contextConfigLocation</param-name>  
  3.   <param-value>classpath:applicationContext.xml</param-value>  
  4.  </context-param>  
  5.  <!-- use our listener to replace the spring listen -->  
  6.  <listener>  
  7.         <listener-class>com.barry.listener.ContextLoaderListener</listener-class>  
  8.     </listener>  
  9.  <!--   
  10.  <listener>  
  11.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  12.  </listener>  
  13.  -->  
  

 

 

ContextLoaderListener.java

 

[java] view plaincopyprint?
  1. packagecom.barry.listener;  
  2.   
  3. import javax.naming.Context;  
  4. import javax.naming.InitialContext;  
  5. import javax.naming.NamingException;  
  6. import javax.servlet.ServletContextEvent;  
  7. import javax.servlet.ServletContextListener;  
  8.   
  9. import org.springframework.web.context.ContextLoader;  
  10.   
  11. /** 
  12.  * Bootstrap listener to start up Spring's root {@link WebApplicationContext}. 
  13.  * Simply delegates to {@link ContextLoader}. 
  14.  *  
  15.  *  
  16.  * @author barry.diao 
  17.  */  
  18. public class ContextLoaderListener implements ServletContextListener  
  19. {  
  20.   
  21.     private ContextLoader contextLoader;  
  22.   
  23.     /** 
  24.      * Initialize the root web application context. 
  25.      * @param event ServletContextEvent 
  26.      */  
  27.     public void contextInitialized(ServletContextEvent event)  
  28.     {  
  29.         Context initCtx;  
  30.         try  
  31.         {  
  32.             initCtx = new InitialContext();  
  33.             Context envCtx = (Context) initCtx.lookup("java:comp/env");  
  34.   
  35.             String config = null;  
  36.             config = (String) envCtx.lookup("yourConfigPath");  
  37.             System.setProperty("yourConfigPath", config);  
  38.   
  39.             envCtx.close();  
  40.             initCtx.close();  
  41.         }  
  42.         catch (NamingException e)  
  43.         {  
  44.             // e.printStackTrace();  
  45.             System.setProperty("yourConfigPath""");  
  46.         }  
  47.         catch (Exception ex)  
  48.         {  
  49.             // ex.printStackTrace();  
  50.             System.setProperty("yourConfigPath""");  
  51.         }  
  52.         this.contextLoader = createContextLoader();  
  53.         this.contextLoader.initWebApplicationContext(event.getServletContext());  
  54.     }  
  55.   
  56.     /** 
  57.      * Create the ContextLoader to use. Can be overridden in subclasses. 
  58.      *  
  59.      * @return the new ContextLoader 
  60.      */  
  61.     protected ContextLoader createContextLoader()  
  62.     {  
  63.         return new ContextLoader();  
  64.     }  
  65.   
  66.     /** 
  67.      * Return the ContextLoader used by this listener. 
  68.      *  
  69.      * @return the current ContextLoader 
  70.      */  
  71.     public ContextLoader getContextLoader()  
  72.     {  
  73.         return this.contextLoader;  
  74.     }  
  75.   
  76.     /** 
  77.      * Close the root web application context. 
  78.      * @param event ServletContextEvent 
  79.      */  
  80.     public void contextDestroyed(ServletContextEvent event)  
  81.     {  
  82.         if (this.contextLoader != null)  
  83.         {  
  84.             this.contextLoader.closeWebApplicationContext(event.getServletContext());  
  85.         }  
  86.     }  
  87.   
  88. }  

 

原创粉丝点击