使用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文件使用的更多相关文章

  1. spring整合mybatis在使用.properties文件时候遇到的问题

    在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致P ...

  2. 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

    在上一篇(详解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(上))博文中已经介绍了关于SSM框架的各种基础配置,(对于SSM配置不熟悉 ...

  3. Spring Boot 的配置文件application.properties

    Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...

  4. 一:Spring Boot 的配置文件 application.properties

    Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...

  5. log4CXX第二篇---配置文件(properties文件)详解

    一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  6. Spring 通过配置文件注入 properties文件

    当我们需要将某些值放入 properties文件 key=value 的方式,获取文件信息使用spring 注入的方式会变得很便捷 1. spring 配置文件需要导入 <?xml versio ...

  7. spring配置文件引入properties文件:<context:property-placeholder>标签使用总结

    一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...

  8. 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(上)

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...

  9. Spring集成Mybatis配置文件的简单理解

    详情可见官方文档http://www.mybatis.org/spring/zh/index.html 一.需要配置的对象实例 1.SqlSessionFactoryBean 在 MyBatis-Sp ...

随机推荐

  1. 用Python利用pyFirmata控制Arduino实现Blink

    2018-03-2809:20:44 arduino中有相应的库 1.安装pyFirmata包 pip install pyFirmata 在python2.7或python3.X下都可以执行. py ...

  2. Python学习笔记之默认参数

    函数定义时 参数定义的顺序必须是:必选参数.默认参数.可变参数和关键字参数. def test(a,b,c=1,*d,**e) pass

  3. Appium基于python unittest自动化测试并生成html测试报告

    本文基于python单元测试框架unittest完成appium自动化测试,生成基于html可视化测试报告 代码示例: #利用unittest并生成测试报告 class Appium_test(uni ...

  4. url-safe base64 && base64

    1: 为什么需要base64? ASCII码一共规定了128个字符的编码,这128个符号,范围在[0,127]之间. 其中,[0,31],及127, 33个属于不可打印的控制字符. 在电子邮件传输信息 ...

  5. 远程图形界面:使用putty+xmin远程登录ubuntu-kde

    让我继续用反人类的编辑器Vim和emacs,我宁愿自断三指.因此,在Win端配置WinSCP+Putty+Xming远程操作ubuntu. 参考链接:putty+xming远程登录Ubuntu16.0 ...

  6. 《网络管理》IP地址管理与子网划分

    IP地址管理——ipmaster ipmaster是一款对IP地址进行管理的软件,使用该软件可以提高网络管理员的工作效率.在大型网络中,使用该软件可以有序且高效地实现大中小型企业网IP地址的分配和管理 ...

  7. The Runtime Interaction Model for Views-UI布局事件处理流程

    The Runtime Interaction Model for Views Any time a user interacts with your user interface, or any t ...

  8. Windows提高_2.1第一部分:线程

    第一部分:线程 什么是线程? 线程其实可以理解为一段正在执行中的代码,它最少由一个线程内核对象和一个栈组成. 线程之间是没有从属关系的,同一进程下的所有线程都可以访问进程内的所有内容. 主线程其实是创 ...

  9. springAOP源码解析-190313

    Spring相关笔记 SpringAOP讲解 子路老师讲解 spring与aspectj的区别答:它们的区别是 spring是动态加载 aspectj是静态加载,再编译过程就已经实现切面,此时会往代码 ...

  10. EXP-00083: 调用 EXFSYS.DBMS_EXPFIL_DEPASEXP.schema_info_exp 时出现前一问题

    select owner,object_name,object_type,status from dba_objects where object_name = 'LT_EXPORT_PKG'; 如果 ...