SpringMVC加载配置Properties文件的几种方式

摘要:
最近开发的项目使用SpringMVC框架。我觉得SpringMVC的代码非常优雅和强大。Internet上有许多关于Controller参数绑定和URL映射的文章。本博客主要总结了SpringMVC加载配置属性文件的几种方式。1.通过context:属性占位符加载配置文件1.1,并在spring.xml中添加与上下文相关的引用1.2。导入jdbc配置文件1.3。jdbc的配置。属性如下:jdbc_driverClassName=com。mysql。jdbc。驱动程序jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8jdbc_username=rootjdbc_password=1234561.4、参考spring mybatis.xml中jdbc中的配置${jdbc_driverClassName}${jdbc_url}${jdbc_username}${jdbc_password}1600003true˂!

最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,

网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式

1.通过context:property-placeholde实现配置文件加载

  1.1、在spring.xml中加入context相关引用

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

 1.2、引入jdbc配置文件         

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

1.3、jdbc.properties的配置如下

   

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

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

  1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"  
  2.    destroy-method="close" >  
  3.    <property name="driverClassName">  
  4.      <value>${jdbc_driverClassName}</value>  
  5.    </property>  
  6.    <property name="url">  
  7.      <value>${jdbc_url}</value>  
  8.    </property>  
  9.    <property name="username">  
  10.      <value>${jdbc_username}</value>  
  11.    </property>  
  12.    <property name="password">  
  13.      <value>${jdbc_password}</value>  
  14.    </property>  
  15.    <!-- 连接池最大使用连接数 -->  
  16.    <property name="maxActive">  
  17.      <value>20</value>  
  18.    </property>  
  19.    <!-- 初始化连接大小 -->  
  20.    <property name="initialSize">  
  21.      <value>1</value>  
  22.    </property>  
  23.    <!-- 获取连接最大等待时间 -->  
  24.    <property name="maxWait">  
  25.      <value>60000</value>  
  26.    </property>  
  27.    <!-- 连接池最大空闲 -->  
  28.    <property name="maxIdle">  
  29.      <value>20</value>  
  30.    </property>  
  31.    <!-- 连接池最小空闲 -->  
  32.    <property name="minIdle">  
  33.      <value>3</value>  
  34.    </property>  
  35.    <!-- 自动清除无用连接 -->  
  36.    <property name="removeAbandoned">  
  37.      <value>true</value>  
  38.    </property>  
  39.    <!-- 清除无用连接的等待时间 -->  
  40.    <property name="removeAbandonedTimeout">  
  41.      <value>180</value>  
  42.    </property>  
  43.    <!-- 连接属性 -->  
  44.    <property name="connectionProperties">  
  45.      <value>clientEncoding=UTF-8</value>  
  46.    </property>  
  47.  </bean>  



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

  1. import org.springframework.beans.factory.annotation.Value;  
  2. import org.springframework.context.annotation.Configuration;  
  3.   
  4.   
  5.    
  6. @Configuration   
  7. public class JdbcConfig{      
  8.       
  9.     @Value("${jdbc_url}")  
  10.     public  String jdbcUrl; //这里变量不能定义成static  
  11.       
  12.     @Value("${jdbc_username}")  
  13.     public  String username;    
  14.       
  15.     @Value("${jdbc_password}")  
  16.     public  String password;    
  17.        
  18. }  



1.6、在controller中调用

   

  1. @RequestMapping("/service/**")  
  2. @Controller  
  3. public class JdbcController{  
  4.    
  5.          @Autowired  
  6.      private JdbcConfig Config; //引用统一的参数配置类  
  7.   
  8.          @Value("${jdbc_url}")  
  9.          private String jdbcUrl; //直接在Controller引用  
  10.          @RequestMapping(value={"/test"})   
  11.         public ModelMap test(ModelMap modelMap) {   
  12.                modelMap.put("jdbcUrl", Config.jdbcUrl);  
  13.                return modelMap;   
  14.           }  
  15.          @RequestMapping(value={"/test2"})  
  16.      public ModelMap test2(ModelMap modelMap) {   
  17.            modelMap.put("jdbcUrl", this.jdbcUrl);  
  18.            return modelMap;  
  19.      }   
  20. }  

1.7、测试

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

返回如下结果:

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

注:通过context:property-placeholde加载多个配置文件

 只需在第1.2步中将多个配置文件以逗号分隔即可 

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

2、通过util:properties实现配置文件加载

 2.1、在spring.xml中加入util相关引用

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:context="http://www.springframework.org/schema/context"  
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.  xmlns:util="http://www.springframework.org/schema/util"  
  6.  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8.       http://www.springframework.org/schema/context  
  9.       http://www.springframework.org/schema/context/spring-context.xsd  
  10.       http://www.springframework.org/schema/util  
  11.       http://www.springframework.org/schema/util/spring-util-4.0.xsd">  

2.2、 引入config配置文件  

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

2.3、config.properties的配置如下 

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

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

  1. import org.springframework.beans.factory.annotation.Value;  
  2. import org.springframework.stereotype.Component;  
  3.   
  4. @Component  
  5. public class Config {   
  6.       @Value("#{settings['gnss.server.url']}")    
  7.       public  String GNSS_SERVER_URL;   
  8.    
  9. }  

2.5、在controller中调用

  1. @RequestMapping("/service2/**")  
  2. @Controller  
  3. public class ConfigController{  
  4.    
  5.          @Autowired  
  6.      private Config Config; //引用统一的参数配置类  
  7.   
  8.          @RequestMapping(value={"/test"})  
  9.      public ModelMap test(ModelMap modelMap) {   
  10.         modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);  
  11.             return modelMap;  
  12.       }   
  13. }  



2.6、测试

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

返回如下结果:

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



3.直接在Java类中通过注解实现配置文件加载

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

  1. import org.springframework.beans.factory.annotation.Value;  
  2. import org.springframework.context.annotation.Configuration;  
  3. import org.springframework.context.annotation.PropertySource;  
  4.   
  5. @Configuration  
  6. @PropertySource(value="classpath:config.properties")    
  7. public class Config {   
  8.    
  9. @Value("${gnss.server.url}")   
  10. public  String GNSS_SERVER_URL;  
  11.   
  12. @Value("${gnss.server.url}")   
  13. public  String jdbcUrl;   
  14.   
  15. }  

3.2、在controller中调用

  1. @RequestMapping("/service2/**")  
  2. @Controller  
  3. public class ConfigController{  
  4.    
  5.          @Autowired  
  6.      private Config Config; //引用统一的参数配置类  
  7.   
  8.           @RequestMapping(value={"/test"})  
  9.      public ModelMap test(ModelMap modelMap) {   
  10.         modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);  
  11.             return modelMap;  
  12.      }   

}

3.3、测试

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

返回如下结果:

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

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

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:context="http://www.springframework.org/schema/context"  
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.  xmlns:util="http://www.springframework.org/schema/util"  
  6.  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8.       http://www.springframework.org/schema/context  
  9.       http://www.springframework.org/schema/context/spring-context.xsd  
  10.       http://www.springframework.org/schema/util  
  11.       http://www.springframework.org/schema/util/spring-util-4.0.xsd">  
  12.   
  13.     <!-- 引入jdbc配置文件 -->  
  14.     <context:property-placeholder location="classpath:jdbc.properties"/>  
  15.   
  16.      <!-- 引入多配置文件 -->  
  17.        <context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>  
  18.      <!-- 通过util引入config配置文件 -->  
  19.      <!-- <util:properties id="settings" location="classpath:config.properties" /> -->  
  20.      <!-- 扫描文件(自动将servicec层注入) -->   
  21.      <context:component-scan base-package="修改成你的Config类所在的package"/></beans>  
业务需求变更永无休止,技术前进就永无止境!

免责声明:文章转载自《SpringMVC加载配置Properties文件的几种方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇亚马逊API之订单下载SVG的几个标签元素下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

NODE_ENV判断node服务器环境的设置

build->webpack.sit.conf.js 拷贝webpack.prod.conf.js,新建webpack.sit.conf.js、webpack.uait.conf.js,根据环境修改env的引入文件 config->sit.env.js build->bulid.js src -> config文件夹 -&...

一个关于vue+mysql+express的全栈项目(二)------ 前端构建

一、使用vue-cli脚手架构建 1 <!-- 全局安装vue-cli --> 2 npm install -g vue-cli 3 <!-- 设置vue webpack模板 --> 4 vue init webpack my-project 5 <!-- 进入项目 --> 6 cd my-project...

1.使用POI结合springmvc实现上传

1.引入poi的依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency...

Android从相册获取图片

应用中经常会有去相册获取图片的需求,每次总是会忘,现在这做个记录,以便以后查阅。 从应用去相册获取图片主要有以下几个步骤: 1、设置intent调用系统相册 2、进入相册选取图片,如果相册没有满意的图片,可以使用相机照相获取,最后返回数据到应用 3、应用获取图片后,调用裁剪程序对图片进行裁剪 4、返回裁剪的图片并显示出来 一、设置intent调用系统相册,...

安卓完全退出程序的六种方法

1. Dalvik VM的本地方法   //杀死进程android.os.Process.killProcess(android.os.Process.myPid())  //抛异常强制退出  System.exit(0);2.任务管理器方法  //通过activity管理器重启ActivityManager activitymanager= (Activ...

MySql连接字符串的说明

MySql连接字符串的说明 下文对MySql连接字符串的相关参数及格式进行了详细的说明,供您参考,如果对您MySql连接字符串感兴趣的话,不妨一看。 mysql JDBC 驱动常用的有两个,一个是gjt(Giant Java Tree)组织提供的mysql驱动,其JDBC Driver名称(JAVA类名)为:org.gjt.mm.mysql.Drive...