over 3 years ago

在Spring中有提供設定檔的工具方便載入,並可以在xml跟java中可以自動注入變數,相當方便

使用PropertyPlaceholderConfigurer或是util:properties

方案一 使用PropertyPlaceholderConfigurer

要使用context元素記得要先加上context定義
xmlns:context="http://www.springframework.org/schema/context"
還有提供xsd對應檔位置
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"

不然Spring不認識context:property-placeholder喔
下面是常用的定義檔

xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-3.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

在spring設定檔中這樣引用

application-beans.xml
<bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="fileEncoding" value="UTF-8" />
    <property name="location" value="classpath:config.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

讀取外部檔案

application-beans.xml
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="fileEncoding" value="UTF-8" />
        <property name="location" value="file:///D:/testtomcat/data0/etc/wechat/jdbc.properties" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>

Spring3.0後可以這樣寫

application-beans.xml
<context:property-placeholder location="classpath:config.properties" file-encoding="UTF-8" ignore-unresolvable="true" />

Loading multiple properties files

application-beans.xml
<bean id="appProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:mail.properties</value>
            <value>classpath:database.properties</value>
        </list>
    </property>
</bean>

如果有多個設定檔則ignore-unresolvable要設成true

就可以在其他設定檔中使用這個參數如下

spring-mybatis.xml
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />
</bean>

或如果想在java中取得此變數可以這樣

@Value("${jdbc_url}")
private String jdbcUrl;

就可以取到一樣的值

但是這變數是有可能重複到互相覆蓋,也不太好辨識在哪個設定檔,你也可以使用方案二

方案二 使用util:properties

在Spring設定檔中增加命名xmlns:util跟spring-util-3.2.xsd,並加上util:properties

application-beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
  xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  
  <util:properties id="securitysource" location="classpath:security.properties" />
</beans>

設定檔

security.properties
sessiontimeout=30

你便可以在java中使用這樣方法獲得變數

@Value("#{securitysource[sessiontimeout]}")
private Long sessiontimeout;
← SPRING中關於設計模式與注入 Spring中使用多國語系 →
 
comments powered by Disqus