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

简介: SpringMVC加载配置Properties文件的几种方式 最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式 1.

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

最近开发的项目使用了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. @Configuration
  4. public class JdbcConfig{
  5.     @Value("${jdbc_url}")
  6.     public  String jdbcUrl; //这里变量不能定义成static
  7.     @Value("${jdbc_username}")
  8.     public  String username;
  9.     @Value("${jdbc_password}")
  10.     public  String password;
  11. }

 

1.6、在controller中调用

 

[html] view plain copy

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

 

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. @Component
  4. public class Config {
  5.       @Value("#{settings['gnss.server.url']}")
  6.       public  String GNSS_SERVER_URL;
  7. }

2.5、在controller中调用

[html] view plain copy

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

 

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. @Configuration
  5. @PropertySource(value="classpath:config.properties")
  6. public class Config {
  7. @Value("${gnss.server.url}")
  8. public  String GNSS_SERVER_URL;
  9. @Value("${gnss.server.url}")
  10. public  String jdbcUrl;
  11. }

3.2、在controller中调用

[java] view plain copy

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

}

 

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.     <!-- 引入jdbc配置文件 -->
  13.     <context:property-placeholder location="classpath:jdbc.properties"/>
  14.      <!-- 引入多配置文件 -->
  15.        <context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
  16.      <!-- 通过util引入config配置文件 -->
  17.      <!-- <util:properties id="settings" location="classpath:config.properties" /> -->
  18.      <!-- 扫描文件(自动将servicec层注入) -->
  19.      <context:component-scan base-package="修改成你的Config类所在的package"/></beans>

 

相关文章
|
7月前
|
Java
09 SpringBoot外部配置加载顺序
09 SpringBoot外部配置加载顺序
47 0
|
7月前
|
Java Spring
08 SpringBoot配置文件加载位置
08 SpringBoot配置文件加载位置
24 0
|
Java 数据库
基于Springboot外卖系统03:pom.xml导入依赖+数据库配置文件+Boot启动类+静态资源映射
基于Springboot外卖系统03:pom.xml导入依赖+数据库配置文件+Boot启动类+静态资源映射
149 0
|
XML JSON 搜索推荐
SpringBoot的配置【配置文件、加载顺序、配置原理】(超详细)上
SpringBoot的配置【配置文件、加载顺序、配置原理】(超详细)
SpringBoot的配置【配置文件、加载顺序、配置原理】(超详细)上
|
JSON Java 数据格式
【SpringBoot】配置文件的加载与属性值的绑定
【SpringBoot】配置文件的加载与属性值的绑定
【SpringBoot】配置文件的加载与属性值的绑定
|
Java Spring
SpringBoot的配置【配置文件、加载顺序、配置原理】(超详细)下
SpringBoot的配置【配置文件、加载顺序、配置原理】(超详细)
SpringBoot的配置【配置文件、加载顺序、配置原理】(超详细)下
|
Java Spring
SSM 项目因为需要加载多个properties配置文件,处理方式
SSM 项目因为需要加载多个properties配置文件,处理方式
204 0
|
XML Java 数据格式
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(三)
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(三)
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(三)
|
XML Java API
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(一)
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(一)
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(一)
|
XML Java 数据格式
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(二)
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(二)
怎样使用Spring的配置文件?带大家一起玩转Spring配置文件(二)