天天看點

Spring加載多個properties檔案1.分不同的XML檔案來加載不同的properties檔案2.以逗号隔開不同的檔案3.用util标簽

spring隻允許有一個<context:property-placeholder/>,如果想加載多個檔案,就會報錯,後加載的檔案無法加載成功,解決方法如下,有三個,任選其一即可:

1.分不同的XML檔案來加載不同的properties檔案

如,applicationContext-dao.xml加載jdbc.properties,

applicationContext-redis.xml加載redis.properties

2.以逗号隔開不同的檔案

如,<context:property-placeholder location="classpath:jdbc.properties,classpath:redis.properties"/>

3.用util标簽

現在頭檔案引入:

xmlns:util="http://www.springframework.org/schema/util"      
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd      

然後:

<util:properties id="xxx" location="classpath:conf.properties"/>      

代碼中:

@Value("#{xxx['MQ']}")
private String MQ;      

注:MQ為conf.properties中key

總結:推薦方法2,方法3,因為方法1有時候不成功