最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中配置和讀取多個Properties文件的方式方法

 更新時間:2017年04月12日 16:32:57   作者:iguiyi  
本篇文章主要介紹了Spring中配置和讀取多個Properties文件的方式方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

一個系統(tǒng)中通常會存在如下一些以Properties形式存在的配置文件

1.數(shù)據(jù)庫配置文件demo-db.properties:

database.url=jdbc:mysql://localhost/smaple 
database.driver=com.mysql.jdbc.Driver 
database.user=root 
database.password=123 

2.消息服務(wù)配置文件demo-mq.properties:

#congfig of ActiveMQ 
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory 
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000 
mq.java.naming.security.principal= 
mq.java.naming.security.credentials= 
jms.MailNotifyQueue.consumer=5 

3.遠程調(diào)用的配置文件demo-remote.properties:

remote.ip=localhost 
remote.port=16800 
remote.serviceName=test 

一、系統(tǒng)中需要加載多個Properties配置文件

應(yīng)用場景:Properties配置文件不止一個,需要在系統(tǒng)啟動時同時加載多個Properties文件。

配置方式:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation=" 
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
   
  <!-- 將多個配置文件讀取到容器中,交給Spring管理 --> 
  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
      <list> 
       <!-- 這里支持多種尋址方式:classpath和file --> 
       <value>classpath:/opt/demo/config/demo-db.properties</value> 
       <!-- 推薦使用file的方式引入,這樣可以將配置和代碼分離 --> 
       <value>file:/opt/demo/config/demo-mq.properties</value> 
       <value>file:/opt/demo/config/demo-remote.properties</value> 
      </list> 
    </property> 
  </bean> 
   
  <!-- 使用MQ中的配置 --> 
  <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
    <property name="environment"> 
      <props> 
        <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop> 
        <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop> 
        <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop> 
        <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop> 
        <prop key="userName">${mq.java.naming.security.principal}</prop> 
        <prop key="password">${mq.java.naming.security.credentials}</prop> 
      </props> 
    </property> 
  </bean> 
</beans> 

 我們也可以將配置中的List抽取出來:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation=" 
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
   
  <!-- 將多個配置文件位置放到列表中 --> 
  <bean id="propertyResources" class="java.util.ArrayList"> 
    <constructor-arg> 
      <list> 
       <!-- 這里支持多種尋址方式:classpath和file --> 
       <value>classpath:/opt/demo/config/demo-db.properties</value> 
       <!-- 推薦使用file的方式引入,這樣可以將配置和代碼分離 --> 
       <value>file:/opt/demo/config/demo-mq.properties</value> 
       <value>file:/opt/demo/config/demo-remote.properties</value> 
      </list> 
    </constructor-arg> 
  </bean> 
   
  <!-- 將配置文件讀取到容器中,交給Spring管理 --> 
  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations" ref="propertyResources" /> 
  </bean> 
   
  <!-- 使用MQ中的配置 --> 
  <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
    <property name="environment"> 
      <props> 
        <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop> 
        <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop> 
        <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop> 
        <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop> 
        <prop key="userName">${mq.java.naming.security.principal}</prop> 
        <prop key="password">${mq.java.naming.security.credentials}</prop> 
      </props> 
    </property> 
  </bean> 
</beans> 

二、整合多工程下的多個分散的Properties

應(yīng)用場景:工程組中有多個配置文件,但是這些配置文件在多個地方使用,所以需要分別加載。

配置如下:

<?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:p="http://www.springframework.org/schema/p" 
  xsi:schemaLocation=" 
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
   
  <!-- 將DB屬性配置文件位置放到列表中 --> 
  <bean id="dbResources" class="java.util.ArrayList"> 
    <constructor-arg> 
    <list> 
      <value>file:/opt/demo/config/demo-db.properties</value> 
    </list> 
    </constructor-arg> 
  </bean> 
 
  <!-- 將MQ屬性配置文件位置放到列表中 --> 
  <bean id="mqResources" class="java.util.ArrayList"> 
    <constructor-arg> 
    <list> 
      <value>file:/opt/demo/config/demo-mq.properties</value> 
    </list> 
    </constructor-arg> 
  </bean> 
   
  <!-- 用Spring加載和管理DB屬性配置文件 --> 
  <bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="order" value="1" /> 
    <property name="ignoreUnresolvablePlaceholders" value="true" />  
    <property name="locations" ref="dbResources" /> 
  </bean> 
   
  <!-- 用Spring加載和管理MQ屬性配置文件 --> 
  <bean id="mqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="order" value="2" /> 
    <property name="ignoreUnresolvablePlaceholders" value="true" />  
    <property name="locations" ref="mqResources" /> 
  </bean> 
   
  <!-- 使用DB中的配置屬性 --> 
  <bean id="rmsDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"  
    p:driverClassName="${demo.db.driver}" p:url="${demo.db.url}" p:username="${demo.db.username}"  
    p:password="${demo.db.password}" pp:maxActive="${demo.db.maxactive}"p:maxWait="${demo.db.maxwait}"  
    p:poolPreparedStatements="true" p:defaultAutoCommit="false"> 
  </bean> 
   
  <!-- 使用MQ中的配置 --> 
  <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
    <property name="environment"> 
      <props> 
        <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop> 
        <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop> 
        <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop> 
        <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop> 
        <prop key="userName">${mq.java.naming.security.principal}</prop> 
        <prop key="password">${mq.java.naming.security.credentials}</prop> 
      </props> 
    </property> 
  </bean> 
</beans> 

 注意:其中order屬性代表其加載順序,而ignoreUnresolvablePlaceholders為是否忽略不可解析的 Placeholder,如配置了多個PropertyPlaceholderConfigurer,則需設(shè)置為true。這里一定需要按照這種方式設(shè)置這兩個參數(shù)。

三、Bean中直接注入Properties配置文件中的值

應(yīng)用場景:Bean中需要直接注入Properties配置文件中的值 。例如下面的代碼中需要獲取上述demo-remote.properties中的值:

public class Client() { 
  private String ip; 
  private String port; 
  private String service; 
} 

配置如下:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="<a  rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/beans</a>" 
 xmlns:xsi="<a  rel="external nofollow" >http://www.w3.org/2001/XMLSchema-instance</a>" 
 xmlns:util="<a  rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/util</a>" 
 xsi:schemaLocation=" 
 <a  rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/beans</a> <a  rel="external nofollow" >http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</a> 
 <a  rel="external nofollow" rel="external nofollow" >http://www.springframework.org/schema/util</a> <a  rel="external nofollow" >http://www.springframework.org/schema/util/spring-util-3.0.xsd</a>"> 
  
 <!-- 這種加載方式可以在代碼中通過@Value注解進行注入,  
 可以將配置整體賦給Properties類型的類變量,也可以取出其中的一項賦值給String類型的類變量 --> 
 <!-- <util:properties/> 標簽只能加載一個文件,當(dāng)多個屬性文件需要被加載的時候,可以使用多個該標簽 --> 
 <util:properties id="remoteSettings" location="file:/opt/demo/config/demo-remote.properties" />  
  
 <!-- <util:properties/> 標簽的實現(xiàn)類是PropertiesFactoryBean, 
 直接使用該類的bean配置,設(shè)置其locations屬性可以達到一個和上面一樣加載多個配置文件的目的 --> 
 <bean id="settings"  
  class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
  <property name="locations"> 
 <list> 
  <value>file:/opt/rms/config/rms-mq.properties</value> 
  <value>file:/opt/rms/config/rms-env.properties</value> 
 </list> 
  </property> 
 </bean> 
</beans> 

Client類中使用Annotation如下:

import org.springframework.beans.factory.annotation.Value; 
 
public class Client() { 
  @Value("#{remoteSettings['remote.ip']}") 
  private String ip; 
  @Value("#{remoteSettings['remote.port']}") 
  private String port; 
  @Value("#{remoteSettings['remote.serviceName']}") 
  private String service; 
} 

四、Bean中存在Properties類型的類變量

應(yīng)用場景:當(dāng)Bean中存在Properties類型的類變量需要以注入的方式初始化

1. 配置方式:我們可以用(三)中的配置方式,只是代碼中注解修改如下

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.beans.factory.annotation.Autowired; 
 
public class Client() { 
  @Value("#{remoteSettings}") 
  private Properties remoteSettings; 
} 

2. 配置方式:也可以使用xml中聲明Bean并且注入

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation=" 
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
   
  <!-- 可以使用如下的方式聲明Properties類型的FactoryBean來加載配置文件,這種方式就只能當(dāng)做Properties屬性注入,而不能獲其中具體的值 --> 
  <bean id="remoteConfigs" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="locations"> 
      <list> 
        <value>file:/opt/demo/config/demo-remote.properties</value> 
      </list> 
    </property> 
  </bean> 
   
  <!-- 遠端調(diào)用客戶端類 --> 
  <bean id="client" class="com.demo.remote.Client"> 
    <property name="properties" ref="remoteConfigs" /> 
  </bean> 
</beans> 

代碼如下:

import org.springframework.beans.factory.annotation.Autowired; 
 
public class Client() { 
  //@Autowired也可以使用 
  private Properties remoteSettings; 
   
  //getter setter 
} 

 五、使用通配符加載多個properties文件

<context:property-placeholder location="file:///${CONFIG_PATH}/*.properties" />

或者

<context:property-placeholder location="classpath*:/*.properties"/>

上述的各個場景在項目群中特別有用,需要靈活的使用上述各種配置方式。以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決mybatis批量更新出現(xiàn)SQL報錯問題

    解決mybatis批量更新出現(xiàn)SQL報錯問題

    這篇文章主要介紹了mybatis批量更新出現(xiàn)SQL報錯,解決辦法也很簡單只需要在application.properties配置文中的數(shù)據(jù)源url后面添加一個參數(shù),需要的朋友可以參考下
    2022-02-02
  • Java實現(xiàn)添加條碼或二維碼到Word文檔

    Java實現(xiàn)添加條碼或二維碼到Word文檔

    這篇文章主要介紹了如何在Word文檔中添加條碼、二維碼。可在文檔正文段落中添加,也可在頁眉頁腳中添加,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧
    2022-05-05
  • 深入理解Java設(shè)計模式之模板方法模式

    深入理解Java設(shè)計模式之模板方法模式

    這篇文章主要介紹了JAVA設(shè)計模式之模板方法模式的的相關(guān)資料,文中示例代碼非常詳細,供大家參考和學(xué)習(xí),感興趣的朋友可以了解
    2021-11-11
  • Java Spring登錄練習(xí)詳解

    Java Spring登錄練習(xí)詳解

    這篇文章主要介紹了Java編程實現(xiàn)spring簡單登錄的練習(xí),具有一定參考價值,需要的朋友可以了解下,希望能夠給你帶來幫助
    2021-10-10
  • java的Console類的使用方法及實例

    java的Console類的使用方法及實例

    這篇文章主要介紹了java的Console類的使用方法及實例的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • MyBatis 多表查詢?nèi)N最常見的寫法

    MyBatis 多表查詢?nèi)N最常見的寫法

    這篇文章主要介紹了MyBatis 多表查詢?nèi)N最常見的寫法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-04-04
  • 在Spring項目中引入高版本依賴并解決低版本沖突問題的解決方法

    在Spring項目中引入高版本依賴并解決低版本沖突問題的解決方法

    在Spring項目的開發(fā)過程中,依賴管理是一個非常重要且復(fù)雜的問題,我們可能需要引入更高版本的依賴來使用新特性或修復(fù)舊版本的Bug,然而,這些高版本依賴可能會與項目中已有的低版本依賴產(chǎn)生沖突,本文將詳細探討如何在Spring中引入高版本依賴,并解決低版本依賴沖突的問題
    2025-03-03
  • 詳解Hibernate緩存與性能優(yōu)化

    詳解Hibernate緩存與性能優(yōu)化

    在hibernate中,提到性能優(yōu)化,很自然地我們就想到了緩存。緩存是什么,都有哪些呢?下面這篇文章就主要給大家介紹了關(guān)于Hibernate緩存與性能優(yōu)化的相關(guān)資料,需要的朋友可以參考下。
    2017-02-02
  • elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實現(xiàn)

    elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實現(xiàn)

    這篇文章主要為大家介紹了elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • Java中的FutureTask實現(xiàn)代碼實例

    Java中的FutureTask實現(xiàn)代碼實例

    這篇文章主要介紹了Java中的FutureTask手寫代碼實例,FutureTask是Future的實現(xiàn),用來異步任務(wù)的獲取結(jié)果,可以啟動和取消異步任務(wù),查詢異步任務(wù)是否計算結(jié)束以及獲取最終的異步任務(wù)的結(jié)果,需要的朋友可以參考下
    2023-12-12

最新評論

古浪县| 石河子市| 庆安县| 蕉岭县| 嘉祥县| 石狮市| 贵南县| 镇远县| 台中市| 汶川县| 桓仁| 炉霍县| 湟源县| 海伦市| 涡阳县| 赣州市| 望奎县| 阿荣旗| 静安区| 洪泽县| 石嘴山市| 丰原市| 广平县| 辽中县| 甘南县| 哈巴河县| 顺平县| 陆丰市| 汾西县| 林周县| 镶黄旗| 新兴县| 新建县| 陇川县| 二手房| 宿州市| 海丰县| 江源县| 南乐县| 平利县| 张家界市|