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

詳解SpringMVC加載配置Properties文件的幾種方式

 更新時間:2017年02月25日 15:03:54   作者:webin  
這篇文章主要介紹了詳解SpringMVC加載配置Properties文件的幾種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

最近開發(fā)的項目使用了SpringMVC的框架,用下來感覺SpringMVC的代碼實現(xiàn)的非常優(yōu)雅,功能也非常強大,

網(wǎng)上介紹Controller參數(shù)綁定、URL映射的文章都很多了,寫這篇博客主要總結(jié)一下SpringMVC加載配置Properties文件的幾種方式

1.通過context:property-placeholde實現(xiàn)配置文件加載

1.1、在spring.xml中加入context相關(guān)引用

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd"> 

 1.2、引入jdbc配置文件        

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

1.3、jdbc.properties的配置如下

jdbc_driverClassName=com.mysql.jdbc.Driver 
jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8 
jdbc_username=root 
jdbc_password=123456 

1.4、在spring-mybatis.xml中引用jdbc中的配置

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" 
  destroy-method="close" > 
  <property name="driverClassName"> 
   <value>${jdbc_driverClassName}</value> 
  </property> 
  <property name="url"> 
   <value>${jdbc_url}</value> 
  </property> 
  <property name="username"> 
   <value>${jdbc_username}</value> 
  </property> 
  <property name="password"> 
   <value>${jdbc_password}</value> 
  </property> 
  <!-- 連接池最大使用連接數(shù) --> 
  <property name="maxActive"> 
   <value>20</value> 
  </property> 
  <!-- 初始化連接大小 --> 
  <property name="initialSize"> 
   <value>1</value> 
  </property> 
  <!-- 獲取連接最大等待時間 --> 
  <property name="maxWait"> 
   <value>60000</value> 
  </property> 
  <!-- 連接池最大空閑 --> 
  <property name="maxIdle"> 
   <value>20</value> 
  </property> 
  <!-- 連接池最小空閑 --> 
  <property name="minIdle"> 
   <value>3</value> 
  </property> 
  <!-- 自動清除無用連接 --> 
  <property name="removeAbandoned"> 
   <value>true</value> 
  </property> 
  <!-- 清除無用連接的等待時間 --> 
  <property name="removeAbandonedTimeout"> 
   <value>180</value> 
  </property> 
  <!-- 連接屬性 --> 
  <property name="connectionProperties"> 
   <value>clientEncoding=UTF-8</value> 
  </property> 
 </bean> 

1.5、在Java類中引用jdbc.properties中的配置

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Configuration; 
@Configuration  
public class JdbcConfig{   
   
  @Value("${jdbc_url}") 
  public String jdbcUrl; //這里變量不能定義成static 
   
  @Value("${jdbc_username}") 
  public String username;  
   
  @Value("${jdbc_password}") 
  public String password;  
    
} 

1.6、在controller中調(diào)用

@RequestMapping("/service/**") 
@Controller 
public class JdbcController{ 
  
     @Autowired 
   private JdbcConfig Config; //引用統(tǒng)一的參數(shù)配置類 
 
     @Value("${jdbc_url}") 
     private String jdbcUrl; //直接在Controller引用 
     @RequestMapping(value={"/test"})  
    public ModelMap test(ModelMap modelMap) {  
        modelMap.put("jdbcUrl", Config.jdbcUrl); 
        return modelMap;  
     } 
     @RequestMapping(value={"/test2"}) 
   public ModelMap test2(ModelMap modelMap) {  
      modelMap.put("jdbcUrl", this.jdbcUrl); 
      return modelMap; 
   }  
} 

1.7、測試

在ie中輸入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2 

返回如下結(jié)果:

{  
  jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8" 
} 

注:通過context:property-placeholde加載多個配置文件

 只需在第1.2步中將多個配置文件以逗號分隔即可

復(fù)制代碼 代碼如下:

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

2、通過util:properties實現(xiàn)配置文件加載

2.1、在spring.xml中加入util相關(guān)引用

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

2.2、 引入config配置文件 

<util:properties id="settings" location="classpath:config.properties"/> 

2.3、config.properties的配置如下

gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest 

2.4、在java類中引用config中的配置  

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Component; 
 
@Component 
public class Config {  
   @Value("#{settings['gnss.server.url']}")  
   public String GNSS_SERVER_URL;  
  
} 

2.5、在controller中調(diào)用

@RequestMapping("/service2/**") 
@Controller 
public class ConfigController{ 
  
     @Autowired 
   private Config Config; //引用統(tǒng)一的參數(shù)配置類 
 
     @RequestMapping(value={"/test"}) 
   public ModelMap test(ModelMap modelMap) {  
    modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL); 
      return modelMap; 
   }  
} 

2.6、測試

在ie中輸入http://localhost:8080/testWeb/service2/test

返回如下結(jié)果:

{ 
  "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest" 
} 

3.直接在Java類中通過注解實現(xiàn)配置文件加載

3.1、在java類中引入配置文件

import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
 
@Configuration 
@PropertySource(value="classpath:config.properties")  
public class Config {  
  
@Value("${gnss.server.url}")  
public String GNSS_SERVER_URL; 
 
@Value("${gnss.server.url}")  
public String jdbcUrl;  
 
} 
3.2、在controller中調(diào)用
@RequestMapping("/service2/**") 
@Controller 
public class ConfigController{ 
  
     @Autowired 
   private Config Config; //引用統(tǒng)一的參數(shù)配置類 
 
     @RequestMapping(value={"/test"}) 
   public ModelMap test(ModelMap modelMap) {  
    modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL); 
      return modelMap; 
   }  
}

3.3、測試

在ie中輸入http://localhost:8080/testWeb/service2/test

返回如下結(jié)果:

{ 
  "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest" 
 } 

最后附上spring.xml的完整配置:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:util="http://www.springframework.org/schema/util" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd 
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 
 
  <!-- 引入jdbc配置文件 --> 
  <context:property-placeholder location="classpath:jdbc.properties"/> 
 
   <!-- 引入多配置文件 --> 
    <context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/> 
   <!-- 通過util引入config配置文件 --> 
   <!-- <util:properties id="settings" location="classpath:config.properties" /> --> 
   <!-- 掃描文件(自動將servicec層注入) -->  
   <context:component-scan base-package="修改成你的Config類所在的package"/></beans> 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot EasyPoi動態(tài)導(dǎo)入導(dǎo)出的兩種方式實現(xiàn)方法詳解

    SpringBoot EasyPoi動態(tài)導(dǎo)入導(dǎo)出的兩種方式實現(xiàn)方法詳解

    項目里使用的是EasyPoi來處理導(dǎo)入導(dǎo)出功能的。近日因業(yè)務(wù)需求調(diào)整,一些導(dǎo)出功能的導(dǎo)出列需要根據(jù)不同的條件動態(tài)導(dǎo)出
    2022-09-09
  • Java中json處理工具JsonPath的使用教程

    Java中json處理工具JsonPath的使用教程

    JsonPath類似于XPath,是一種json數(shù)據(jù)結(jié)構(gòu)節(jié)點定位和導(dǎo)航表達式語言,這篇文章主要為大家介紹了JsonPath的基本使用,需要的小伙伴可以參考下
    2023-08-08
  • 火遍全網(wǎng)的Hutool使用Builder模式創(chuàng)建線程池的方法

    火遍全網(wǎng)的Hutool使用Builder模式創(chuàng)建線程池的方法

    這篇文章主要介紹了火遍全網(wǎng)的Hutool使用Builder模式創(chuàng)建線程池的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Spring Boot2如何構(gòu)建可部署的war包

    Spring Boot2如何構(gòu)建可部署的war包

    這篇文章主要介紹了Spring Boot2如何構(gòu)建可部署的war包,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 解決spring-integration-mqtt頻繁報Lost connection錯誤問題

    解決spring-integration-mqtt頻繁報Lost connection錯誤問題

    這篇文章主要介紹了解決spring-integration-mqtt頻繁報Lost connection錯誤問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Springboot實現(xiàn)人臉識別與WebSocket長連接的實現(xiàn)代碼

    Springboot實現(xiàn)人臉識別與WebSocket長連接的實現(xiàn)代碼

    這篇文章主要介紹了Springboot實現(xiàn)人臉識別與WebSocket長連接的實現(xiàn),本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11
  • Java使用BigDecimal解決小數(shù)計算問題

    Java使用BigDecimal解決小數(shù)計算問題

    Java中的BigDecimal是一個內(nèi)置類,用于精確表示任意大小的十進制數(shù),它提供了一種處理浮點運算精度問題的方法,特別適合金融、貨幣交易等需要高精度計算的場景,本文給大家介紹了java中如何使用BigDecimal解決小數(shù)計算問題,需要的朋友可以參考下
    2024-08-08
  • Springboot實現(xiàn)密碼的加密解密

    Springboot實現(xiàn)密碼的加密解密

    這篇文章主要為大家詳細介紹了Springboot實現(xiàn)密碼的加密解密,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • IP查詢系統(tǒng)的異步回調(diào)案例

    IP查詢系統(tǒng)的異步回調(diào)案例

    本文主要分享了IP查詢系統(tǒng)的異步回調(diào)案例,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • Spring定時任務(wù)注解@Scheduled詳解

    Spring定時任務(wù)注解@Scheduled詳解

    這篇文章主要介紹了Spring定時任務(wù)注解@Scheduled詳解,@Scheduled注解是包org.springframework.scheduling.annotation中的一個注解,主要是用來開啟定時任務(wù),本文提供了部分實現(xiàn)代碼與思路,需要的朋友可以參考下
    2023-09-09

最新評論

翁源县| 体育| 渝北区| 防城港市| 东阳市| 遂宁市| 自贡市| 潼关县| 巴林左旗| 马山县| 阿勒泰市| 磐石市| 宿迁市| 藁城市| 大名县| 惠水县| 周宁县| 泗洪县| 东乡族自治县| 遂溪县| 吴忠市| 化德县| 怀安县| 镇康县| 乌拉特中旗| 武安市| 商丘市| 洪泽县| 河津市| 保山市| 获嘉县| 绥化市| 东乡族自治县| 竹溪县| 安平县| 定南县| 长阳| 道孚县| 博乐市| 邹城市| 泽州县|