1. 最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,
  2.  
  3. 网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式
  4.  
  5. 通过读取Config文件的配置
    例如:
  1. Map<String, String> group = ConfigurationManager.GetConfiguration("config1");
  2.  
  3. this.setBcpApi(group.get("BCP.Webapi"));
  4.  
  5. this.setAppCode(group.get("BCP.AppCode"));
  6.  
  7. this.setGetCustomerApi(group.get("GetCustomer"));
  1.  
  2. 1.通过context:property-placeholde实现配置文件加载
  3.  
  4. 1.1、在spring.xml中加入context相关引用
  5.  
  6. [html] view plain copy
  7. <?xml version="1.0" encoding="UTF-8"?>
  8. <beans xmlns="http://www.springframework.org/schema/beans"
  9. xmlns:context="http://www.springframework.org/schema/context"
  10. xsi:schemaLocation="http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd">
  14.  
  15. 1.2、引入jdbc配置文件
  16.  
  17. [html] view plain copy
  18. <context:property-placeholder location="classpath:jdbc.properties"/>
  19. 1.3jdbc.properties的配置如下
  20.  
  21. [html] view plain copy
  22. jdbc_driverClassName=com.mysql.jdbc.Driver
  23. jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8
  24. jdbc_username=root
  25. jdbc_password=123456
  26.  
  27. 1.4、在spring-mybatis.xml中引用jdbc中的配置
  28.  
  29. [html] view plain copy
  30. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
  31. destroy-method="close" >
  32. <property name="driverClassName">
  33. <value>${jdbc_driverClassName}</value>
  34. </property>
  35. <property name="url">
  36. <value>${jdbc_url}</value>
  37. </property>
  38. <property name="username">
  39. <value>${jdbc_username}</value>
  40. </property>
  41. <property name="password">
  42. <value>${jdbc_password}</value>
  43. </property>
  44. <!-- 连接池最大使用连接数 -->
  45. <property name="maxActive">
  46. <value>20</value>
  47. </property>
  48. <!-- 初始化连接大小 -->
  49. <property name="initialSize">
  50. <value>1</value>
  51. </property>
  52. <!-- 获取连接最大等待时间 -->
  53. <property name="maxWait">
  54. <value>60000</value>
  55. </property>
  56. <!-- 连接池最大空闲 -->
  57. <property name="maxIdle">
  58. <value>20</value>
  59. </property>
  60. <!-- 连接池最小空闲 -->
  61. <property name="minIdle">
  62. <value>3</value>
  63. </property>
  64. <!-- 自动清除无用连接 -->
  65. <property name="removeAbandoned">
  66. <value>true</value>
  67. </property>
  68. <!-- 清除无用连接的等待时间 -->
  69. <property name="removeAbandonedTimeout">
  70. <value>180</value>
  71. </property>
  72. <!-- 连接属性 -->
  73. <property name="connectionProperties">
  74. <value>clientEncoding=UTF-8</value>
  75. </property>
  76. </bean>
  77.  
  78. 1.5、在java类中引用jdbc.properties中的配置
  79.  
  80. [html] view plain copy
  81. import org.springframework.beans.factory.annotation.Value;
  82. import org.springframework.context.annotation.Configuration;
  83.  
  84. @Configuration
  85. public class JdbcConfig{
  86.  
  87. @Value("${jdbc_url}")
  88. public String jdbcUrl; //这里变量不能定义成static
  89.  
  90. @Value("${jdbc_username}")
  91. public String username;
  92.  
  93. @Value("${jdbc_password}")
  94. public String password;
  95.  
  96. }
  97.  
  98. 1.6、在controller中调用
  99.  
  100. [html] view plain copy
  101. @RequestMapping("/service/**")
  102. @Controller
  103. public class JdbcController{
  104.  
  105. @Autowired
  106. private JdbcConfig Config; //引用统一的参数配置类
  107.  
  108. @Value("${jdbc_url}")
  109. private String jdbcUrl; //直接在Controller引用
  110. @RequestMapping(value={"/test"})
  111. public ModelMap test(ModelMap modelMap) {
  112. modelMap.put("jdbcUrl", Config.jdbcUrl);
  113. return modelMap;
  114. }
  115. @RequestMapping(value={"/test2"})
  116. public ModelMap test2(ModelMap modelMap) {
  117. modelMap.put("jdbcUrl", this.jdbcUrl);
  118. return modelMap;
  119. }
  120. }
  121.  
  122. 1.7、测试
  123.  
  124. ie中输入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2
  125.  
  126. 返回如下结果:
  127.  
  128. [java] view plain copy
  129. {
  130. jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"
  131. }
  132.  
  133. 注:通过context:property-placeholde加载多个配置文件
  134.  
  135. 只需在第1.2步中将多个配置文件以逗号分隔即可
  136.  
  137. [html] view plain copy
  138. <context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
  139.  
  140. 2、通过util:properties实现配置文件加载
  141.  
  142. 2.1、在spring.xml中加入util相关引用
  143.  
  144. [html] view plain copy
  145. <?xml version="1.0" encoding="UTF-8"?>
  146. <beans xmlns="http://www.springframework.org/schema/beans"
  147. xmlns:context="http://www.springframework.org/schema/context"
  148. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  149. xmlns:util="http://www.springframework.org/schema/util"
  150. xsi:schemaLocation="http://www.springframework.org/schema/beans
  151. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  152. http://www.springframework.org/schema/context
  153. http://www.springframework.org/schema/context/spring-context.xsd
  154. http://www.springframework.org/schema/util
  155. http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  156. 2.2 引入config配置文件
  157.  
  158. [html] view plain copy
  159. <util:properties id="settings" location="classpath:config.properties"/>
  160.  
  161. 2.3config.properties的配置如下
  162.  
  163. [html] view plain copy
  164. gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest
  165.  
  166. 2.4、在java类中引用config中的配置
  167. [html] view plain copy
  168. import org.springframework.beans.factory.annotation.Value;
  169. import org.springframework.stereotype.Component;
  170.  
  171. @Component
  172. public class Config {
  173. @Value("#{settings['gnss.server.url']}")
  174. public String GNSS_SERVER_URL;
  175.  
  176. }
  177. 2.5、在controller中调用
  178.  
  179. [html] view plain copy
  180. @RequestMapping("/service2/**")
  181. @Controller
  182. public class ConfigController{
  183.  
  184. @Autowired
  185. private Config Config; //引用统一的参数配置类
  186.  
  187. @RequestMapping(value={"/test"})
  188. public ModelMap test(ModelMap modelMap) {
  189. modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);
  190. return modelMap;
  191. }
  192. }
  193.  
  194. 2.6、测试
  195.  
  196. ie中输入http://localhost:8080/testWeb/service2/test
  197.  
  198. 返回如下结果:
  199.  
  200. [html] view plain copy
  201. {
  202. "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
  203. }
  204.  
  205. 3.直接在Java类中通过注解实现配置文件加载
  206.  
  207. 3.1、在java类中引入配置文件
  208.  
  209. [java] view plain copy
  210. import org.springframework.beans.factory.annotation.Value;
  211. import org.springframework.context.annotation.Configuration;
  212. import org.springframework.context.annotation.PropertySource;
  213.  
  214. @Configuration
  215. @PropertySource(value="classpath:config.properties")
  216. public class Config {
  217.  
  218. @Value("${gnss.server.url}")
  219. public String GNSS_SERVER_URL;
  220.  
  221. @Value("${gnss.server.url}")
  222. public String jdbcUrl;
  223.  
  224. }
  225. 3.2、在controller中调用
  226. [java] view plain copy
  227. @RequestMapping("/service2/**")
  228. @Controller
  229. public class ConfigController{
  230.  
  231. @Autowired
  232. private Config Config; //引用统一的参数配置类
  233.  
  234. @RequestMapping(value={"/test"})
  235. public ModelMap test(ModelMap modelMap) {
  236. modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);
  237. return modelMap;
  238. }
  239. }
  240. 3.3、测试
  241.  
  242. ie中输入http://localhost:8080/testWeb/service2/test
  243.  
  244. 返回如下结果:
  245.  
  246. [java] view plain copy
  247. {
  248. "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
  249. }
  250.  
  251. 最后附上spring.xml的完整配置:
  252.  
  253. [html] view plain copy
  254. <?xml version="1.0" encoding="UTF-8"?>
  255. <beans xmlns="http://www.springframework.org/schema/beans"
  256. xmlns:context="http://www.springframework.org/schema/context"
  257. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  258. xmlns:util="http://www.springframework.org/schema/util"
  259. xsi:schemaLocation="http://www.springframework.org/schema/beans
  260. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  261. http://www.springframework.org/schema/context
  262. http://www.springframework.org/schema/context/spring-context.xsd
  263. http://www.springframework.org/schema/util
  264. http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  265.  
  266. <!-- 引入jdbc配置文件 -->
  267. <context:property-placeholder location="classpath:jdbc.properties"/>
  268.  
  269. <!-- 引入多配置文件 -->
  270. <context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
  271. <!-- 通过util引入config配置文件 -->
  272. <!-- <util:properties id="settings" location="classpath:config.properties" /> -->
  273. <!-- 扫描文件(自动将servicec层注入) -->
  274. <context:component-scan base-package="修改成你的Config类所在的package"/></beans>
  275. 个人分类: java web

  

SpringMVC加载配置Properties文件的几种方式的更多相关文章

  1. spring加载hibernate映射文件的几种方式。转自:http://blog.csdn.net/huiwenjie168/article/details/7013618

    在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionF ...

  2. spring加载hibernate映射文件的几种方式 (转)

    在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个 Bean实例中进行的,若配置的映射文件较少时,可以用session ...

  3. spring加载hibernate映射文件的几种方式

    1. 在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessi ...

  4. Java读取Properties文件 Java加载配置Properties文件

    static{ Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader() ...

  5. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

  6. VC中加载LIB库文件的三种方法

    VC中加载LIB库文件的三种方法 在VC中加载LIB文件的三种方法如下: 方法1:LIB文件直接加入到工程文件列表中   在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中&quo ...

  7. Spring加载properties文件的两种方式

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...

  8. Java 读取 .properties 文件的几种方式

    Java 读取 .properties 配置文件的几种方式   Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 ...

  9. 【转载】加密Spring加载的Properties文件

    目标:要加密spring的jdbc配置文件的密码口令. 实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密. 1.扩展 package com.rail.comm; import j ...

随机推荐

  1. Java基础93 JDBC连接MySQL数据库

    本文知识点(目录): 1.什么是jdbc     2.jdbc接口的核心API     3.使用JDBC技术连接MySQL数据库的方法    4.使用Statement执行sql语句(DDL.DML. ...

  2. Coursera台大机器学习技法课程笔记05-Kernel Logistic Regression

    这一节主要讲的是如何将Kernel trick 用到 logistic regression上. 从另一个角度来看soft-margin SVM,将其与 logistic regression进行对比 ...

  3. python之抽象基类

    抽象基类特点 1.不能够实例化 2.在这个基础的类中设定一些抽象的方法,所有继承这个抽象基类的类必须覆盖这个抽象基类里面的方法 思考 既然python中有鸭子类型,为什么还要使用抽象基类? 一是我们在 ...

  4. hdu5646数学构造+二分

    /* 满足n>=(k+1)*k/2的整数n必定满足 a+(a+1)+...+(a+k-1)<=n<=(a+1)+(a+2)+...+(a+k) 只要在[a,a+k]中减掉一个数字ai ...

  5. cf Queries on a String

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; #define ...

  6. python接口自动化测试十七:使用bs4框架进行简单的爬虫

    安装:beautifulsoup4 from bs4 import BeautifulSoup yoyo = open('yoyo.html', 'r')   # 以读的方式打开“yoyo.html” ...

  7. 转载 c++指针 指针入门

    这是一篇我所见过的关于指针的最好的入门级文章,它可使初学者在很短的时间内掌握复杂的指针操作.虽然,现在的Java.C#等语言已经取消了指针,但作为一个C++程序员,指针的直接操作内存,在数据操作方面有 ...

  8. HDU 1542 矩形面积并【离散化+线段树+扫描线】

    <题目链接> 题目大意: 给你n个矩形,求出它们面积的并. 解题分析: 此题主要用到了扫描线的思想,现将各个矩形的横坐标离散化,然后用它们离散化后的横坐标(相当于将矩形的每条竖线投影在x轴 ...

  9. Python的getpass模块

    Python的getpass模块 目录 简单介绍 getpass() getuser() 简单介绍 getpass模块提供了两个函数: getpass() 获取输入的密码,并且输入内容屏幕不显示,和L ...

  10. Angular 个人深究(五)【外部包引用 Leaflet 简单实用】

    Leaflet 使用 最近在Angular项目中,用到了地图,由于种种原因放弃了百度地图api使用,最后选择了leaflet,简单介绍一下. 介绍: Leaflet 是一个为移动设备设计的交互式地图的 ...