SpringMVC加载配置Properties文件的几种方式
最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式 通过读取Config文件的配置
例如:
Map<String, String> group = ConfigurationManager.GetConfiguration("config1");
this.setBcpApi(group.get("BCP.Webapi"));
this.setAppCode(group.get("BCP.AppCode"));
this.setGetCustomerApi(group.get("GetCustomer"));
1.通过context:property-placeholde实现配置文件加载 1.1、在spring.xml中加入context相关引用 [html] view plain copy
<?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配置文件 [html] view plain copy
<context:property-placeholder location="classpath:jdbc.properties"/>
1.3、jdbc.properties的配置如下 [html] view plain copy
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中的配置 [html] view plain copy
<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>
<!-- 连接池最大使用连接数 -->
<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中的配置 [html] view plain copy
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中调用 [html] view plain copy
@RequestMapping("/service/**")
@Controller
public class JdbcController{ @Autowired
private JdbcConfig Config; //引用统一的参数配置类 @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 返回如下结果: [java] view plain copy
{
jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"
} 注:通过context:property-placeholde加载多个配置文件 只需在第1.2步中将多个配置文件以逗号分隔即可 [html] view plain copy
<context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/> 2、通过util:properties实现配置文件加载 2.1、在spring.xml中加入util相关引用 [html] view plain copy
<?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配置文件 [html] view plain copy
<util:properties id="settings" location="classpath:config.properties"/> 2.3、config.properties的配置如下 [html] view plain copy
gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest 2.4、在java类中引用config中的配置
[html] view plain copy
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中调用 [html] view plain copy
@RequestMapping("/service2/**")
@Controller
public class ConfigController{ @Autowired
private Config Config; //引用统一的参数配置类 @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 返回如下结果: [html] view plain copy
{
"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
} 3.直接在Java类中通过注解实现配置文件加载 3.1、在java类中引入配置文件 [java] view plain copy
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中调用
[java] view plain copy
@RequestMapping("/service2/**")
@Controller
public class ConfigController{ @Autowired
private Config Config; //引用统一的参数配置类 @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 返回如下结果: [java] view plain copy
{
"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
} 最后附上spring.xml的完整配置: [html] view plain copy
<?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>
个人分类: java web
SpringMVC加载配置Properties文件的几种方式的更多相关文章
- spring加载hibernate映射文件的几种方式。转自:http://blog.csdn.net/huiwenjie168/article/details/7013618
在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionF ...
- spring加载hibernate映射文件的几种方式 (转)
在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个 Bean实例中进行的,若配置的映射文件较少时,可以用session ...
- spring加载hibernate映射文件的几种方式
1. 在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessi ...
- Java读取Properties文件 Java加载配置Properties文件
static{ Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader() ...
- Eclipse下SpringBoot没有自动加载application.properties文件
Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...
- VC中加载LIB库文件的三种方法
VC中加载LIB库文件的三种方法 在VC中加载LIB文件的三种方法如下: 方法1:LIB文件直接加入到工程文件列表中 在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中&quo ...
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- Java 读取 .properties 文件的几种方式
Java 读取 .properties 配置文件的几种方式 Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 ...
- 【转载】加密Spring加载的Properties文件
目标:要加密spring的jdbc配置文件的密码口令. 实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密. 1.扩展 package com.rail.comm; import j ...
随机推荐
- su和sudo命令详解
我们知道,在Linux下对很多文件进行修改都需要有root(管理员)权限,比如对/ect/profile等文件的修改.很多情况下,我们在进行开发的时候都是使用普通用户进行登录的,尤其在进行一些环境变量 ...
- raindi python魔法函数(一)之__repr__与__str__
__repr__和__str__都是python中的特殊方法,都是用来输出实例对象的,如果没有定义这两个方法在打印的时候只会输出实例所在的内存地址 这种方式的输出没有可读性,并不能直观的体现实例.py ...
- (七)CXF添加拦截器
今天开始讲下拦截器,前面大家学过servlet,struts2 都有拦截器概念,主要作用是做一些权限过滤,编码处理等: webservice也可以加上拦截器,我们可以给webservice请求加权限判 ...
- python 全栈开发,Day111(客户管理之 编辑权限(二),Django表单集合Formset,ORM之limit_choices_to,构造家族结构)
昨日内容回顾 1. 权限系统的流程? 2. 权限的表有几个? 3. 技术点 中间件 session orm - 去重 - 去空 inclusion_tag filter 有序字典 settings配置 ...
- k8s单机部署1.11.5
一.概述 由于服务器有限,因此只能用虚拟机搭建 k8s.但是开3个节点,电脑卡的不行. k8s中文社区封装了一个 Minikube,用来搭建单机版,链接如下: https://yq.aliyun.co ...
- Linux学习笔记:使用ftp命令上传和下载文件
Linux中如何使用ftp命令,包括如何连接ftp服务器,上传or下载文件以及创建文件夹.虽然现在有很多ftp桌面应用(例如:FlashFXP),但是在服务器.SSH.远程会话中掌握命令行ftp的使用 ...
- whiledo循环输出数组中的分数
var scores = [24, 32, 17]; var arrayLength = scores.length; var i =0; while(i < arrayLength){ var ...
- 【C++ Primer 第16章】2. 模板实参推断
模板实参推断:对于函数模板,编译器利用调用中的函数实参来确定模板参数,从函数实参来确定模板参数的过程被称为模板实参推断. 类型转换与模板类型参数 与往常一样,顶层const无论在形参中还是在是实参中, ...
- ubuntu的常用liunx命令
一.基本命令 1.查看Ubuntu版本 $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Descriptio ...
- Windows 7下载安装 Visual C++ 6.0(VC6) 全程图解
说实话我也一直没有试过,所以也想当然的认为Win7下就不能安装VC6,压根就100%不兼容?一直使用高版本的VS(如VS2008和现在用的VS2010)的我今天亲身在Win7下安装一次试试. 注:文中 ...