Spring/Maven/MyBatis配置文件结合properties文件使用
使用properties文件也叫注入,比如把一些常用的配置项写入到这个文件,然后在Spring的XML配置文件中使用EL表达式去获取。
这种方式不只Spring可以使用,同样MyBatis也可以使用,只不过加载的方式不一样,但是获取值同样是EL表达式。具体的参考官方文档。
properties语法参考:https://zh.wikipedia.org/wiki/.properties,注意转移字符。
Spring:
本次使用的例子来自这章http://www.cnblogs.com/EasonJim/p/7055499.html,通过改造实现将数据库连接信息全部使用properties去配置。
前提:
1、新建db.properties文件,加入如下配置:
jdbc.username=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=UTF-8&serverTimezone\=UTC
注意:在定义这些变量的时候,尽量避免一些系统变量,比如username,这个代表操作系统的用户名。
2、在加载properties文件路径上,使用的是classpath加通配符*去实现扫描项目的classes目录下的文件来加载,不再是绝对路径,具体的用法参考:http://www.cnblogs.com/EasonJim/p/6709314.html
下面将列举加载方式:
提示:在获取值时,通常可以为其设置默认值,比如${timeout:100}
一、<context:property-placeholder />
1、在配置Spring的Bean文件时,比如引入头声明,如下所示:
...
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
...
2、Bean配置文件加入:
<context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties" />
3、获取变量:
<!--采用DBCP连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
二、<util:properties />
1、在配置Spring的Bean文件时,比如引入头声明,如下所示:
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
2、Bean配置文件加入:
<util:properties id="db" location="classpath:db.properties"/>
3、获取变量:
<!--采用DBCP连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="#{db['jdbc.driver']}" />
<property name="url" value="#{db['jdbc.url']}" />
<property name="username" value="#{db['jdbc.username']}" />
<property name="password" value="#{db['jdbc.password']}" />
</bean>
三、通过代码注入实现代码上可以获取这些变量(@Value)
1、Bean配置文件加入:
<!-- 使用注解注入properties中的值 -->
<bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
<!-- 设置编码格式 -->
<property name="fileEncoding" value="UTF-8"></property>
</bean>
2、在需要测试的类中写入如下代码,但是这个类必须是通过Bean注入过的,做直接的使用就是MVC的Controller。
package com.jsoft.testmybatis.controller; import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.jsoft.testmybatis.inter.IUserOperation;
import com.jsoft.testmybatis.models.Article; @Controller
@RequestMapping("/article")
public class UserController { @Value("#{config['jdbc.username']}")
private String userName; @Value("#{config['jdbc.password']}")
private String password; @Value("#{config['jdbc.url']}")
private String url; @Value("#{config['jdbc.driver']}")
private String driver; @Autowired
IUserOperation userMapper; @RequestMapping("/list")
public ModelAndView listall(HttpServletRequest request,HttpServletResponse response){ System.out.println("测试:"+this.userName+"-"+this.password+"-"+this.url+"-"+this.driver); List<Article> articles=userMapper.getUserArticles(1);
ModelAndView mav=new ModelAndView("/article/list");
mav.addObject("articles",articles);
return mav;
}
}
3、另一种Bean实现,定义

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>

4、使用@value注解获取配置文件的值
@Value("${isenable}")
private Boolean isEnable;
Maven:
在Maven使用很有帮助,比如多模块项目时,在Parent模块上定一个总的属性,每个模块引入之后,到时需要修改也只是一处地方。
一、直接在POM中使用properties定义属性
1、POM
<!-- 项目属性 -->
<properties>
<!-- main version setting -->
<servlet.version>3.1.0</servlet.version>
</properties>
2、使用:
<!-- Servlet Library -->
<!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
二、如果对于引入文件基本是无解,只能在POM中定义。
MyBatis:
一、通过properties节点引入:
1、在MyBatis配置文件下引入:
...
<configuration> <!-- 引入properties配置文件 -->
<properties resource="config.properties" />
...
2、使用:
<!-- 配置环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test21
参考:
http://www.cnblogs.com/BensonHe/p/3963940.html
http://blog.csdn.net/csujiangyu/article/details/50945486
http://xitongjiagoushi.blog.51cto.com/9975742/1659051/
http://825635381.iteye.com/blog/2196906
http://www.cnblogs.com/atliwen/p/5729670.html
Spring/Maven/MyBatis配置文件结合properties文件使用的更多相关文章
- spring整合mybatis在使用.properties文件时候遇到的问题
在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致P ...
- 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
在上一篇(详解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(上))博文中已经介绍了关于SSM框架的各种基础配置,(对于SSM配置不熟悉 ...
- Spring Boot 的配置文件application.properties
Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...
- 一:Spring Boot 的配置文件 application.properties
Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...
- log4CXX第二篇---配置文件(properties文件)详解
一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...
- Spring 通过配置文件注入 properties文件
当我们需要将某些值放入 properties文件 key=value 的方式,获取文件信息使用spring 注入的方式会变得很便捷 1. spring 配置文件需要导入 <?xml versio ...
- spring配置文件引入properties文件:<context:property-placeholder>标签使用总结
一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...
- 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(上)
SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...
- Spring集成Mybatis配置文件的简单理解
详情可见官方文档http://www.mybatis.org/spring/zh/index.html 一.需要配置的对象实例 1.SqlSessionFactoryBean 在 MyBatis-Sp ...
随机推荐
- 2017杭电多校第五场11Rikka with Competition
Rikka with Competition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- 向Linus学习,让代码具有good taste
在最近关于 Linus Torvalds 的一个采访中,这位 Linux 的创始人,在采访过程中大约 14:20 的时候,提及了关于代码的 “good taste”.good taste?采访者请他展 ...
- c++类的内存布局
问题: 考察了reinterpret_cast和static_cast的区别.顺道发现了一个可以查看c++内存布局的工具(在VS中). 结果: 前两个输出的地址形同,后一个不同. class A{in ...
- 387 First Unique Character in a String 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...
- Mysql动态查询
if条件查询 格式: <if test=”条件判断”> 添加到sql的语句 </if> where标签 简化SQL语句中WHERE条件判断 智能处理and和or 如果使用几个i ...
- BPI-MI1刷Andorid的启动卡之后上网的步骤(以太网&&WIFI)
BPI-MI1刷Andorid的启动卡之后上网的步骤(以太网&&WIFI) 2017/9/19 16:57 01刷Android的默认启动界面.png 02打开英文模式下的设置Sett ...
- IBatis的分页研究
IBatis的分页研究 博客分类: Ibatis学习 摘自: http://cpu.iteye.com/blog/311395 yangtingkun Oracle分页查询语句 ibaits. ...
- 15年第六届蓝桥杯第七题_(string)
手链样式 小明有3颗红珊瑚,4颗白珊瑚,5颗黄玛瑙.他想用它们串成一圈作为手链,送给女朋友.现在小明想知道:如果考虑手链可以随意转动或翻转,一共可以有多少不同的组合样式呢? 请你提交该整数.不要填写任 ...
- MFC_2.6 使用菜单列表和控件
使用菜单列表和控件 1.添加List Control控件 2.属性设置VIEW 为REPORT 3.初始化 // 1. 设置列表的扩展风格 m_ListCtrl.SetExtendedStyle(LV ...
- ThinkPHP---thinkphp完善站内信功能
[一]收件箱 分析 控制器:EmailController.class.php 方法:recBox(全称receive box收件箱) 模板文件:recBox.html 分步操作: 第一步:创建方法r ...