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

转载请说明出处http://blog.csdn.net/chinadim/article/details/40621671


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

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


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

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

[html]  view plain  copy
  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配置文件         

[html]  view plain  copy
  1. <context:property-placeholder location="classpath:jdbc.properties"/>  

1.3、jdbc.properties的配置如下

   

[html]  view plain  copy
  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中的配置

[html]  view plain  copy
  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中的配置

[html]  view plain  copy
  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中调用

   

[html]  view plain  copy
  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  

返回如下结果:

[java]  view plain  copy
  1. {   
  2.     jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"  
  3. }  

 

 

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

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

[html]  view plain  copy
  1. <context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>  
 

 

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

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

[html]  view plain  copy
  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配置文件  

[html]  view plain  copy
  1. <util:properties id="settings" location="classpath:config.properties"/>  


2.3、config.properties的配置如下 

[html]  view plain  copy
  1. gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest  

2.4、在java类中引用config中的配置   
[html]  view plain  copy
  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中调用

[html]  view plain  copy
  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

返回如下结果:

[html]  view plain  copy
  1. {  
  2.    "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"  
  3. }  


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

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

[java]  view plain  copy
  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中调用

[java]  view plain  copy
  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

返回如下结果:

[java]  view plain  copy
  1. {  
  2.    "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"  
  3.  }  

 
 

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

[html]  view plain  copy
  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>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值