1.使用外部属性文件

在配置文件里配置Bean时,有时需要在Bean的配置文件里引入系统部署的细节信息(例如:文件的路径、数据源配置信息等),而这些部署细节实际上需要和bean配置相分离,因为我们修改一次properties文件的代价要远远低于修改spring.xml。

为了满足在bean配置时引入配置文件中的信息,Spring提供一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将从外部加载的配置文件信息放到bean的属性当中,可以在Bean配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性来替换这些带$的变量。

首先我们在classpath下创建一个db.properties的数据库配置文件:

 user=root
password=123456
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test

重新引入spring的命名空间:

 <?xml version="1.0" encoding="UTF-8"?>
<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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">

之后我们通过<context>标签将配置文件引进来:

 <!--导入属性配置文件-->
<context:property-placeholder location="classpath:db.properties"/>

接下来建立一个与properties文件属性的应对DataSource类,并生成属性对应的setter方法和toString方法。

 /**
* @author wzy
* @version 1.0
* @date 2019/5/8 17:18
*/
public class DataSource {
private String user;
private String password;
private String driverClass;
private String jdbcUrl; public void setUser(String user) {
this.user = user;
} public void setPassword(String password) {
this.password = password;
} public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
} public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
@Override
public String toString() {
return "DataSource{" +
"user='" + user + '\'' +
", password='" + password + '\'' +
", driverClass='" + driverClass + '\'' +
", jdbcUrl='" + jdbcUrl + '\'' +
'}';
}
}

继续编写spring.xml配置文件,声明一个DataSource类的Bean,并且使用${属性名}的方式从配置文件中取属性。

 <bean id="dataSource" class="com.wzy.properties.DataSource">
<!--使用外部化属性文件的属性-->
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<property name="driverClass" value="${driverclass}"/>
<property name="jdbcUrl" value="${jdbcurl}"/>
</bean>

编写测试类Main:

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException; /**
* @author wzy
* @version 1.0
* @date 2019/5/7 15:58
*/
public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource = (DataSource) ctx.getBean("dataSource"); System.out.println(dataSource); }
}

执行结果:

我们可以看到配置文件中的属性成功的注入到了dataSource这个bean当中,我们可以总结出spring引用properties文件中的属性时,需要两步:

1.使用<context:property-placeholder location="classpath:db.properties"/>标签引入配置文件位置。

2.使用${属性名}的方式对配置文件的属性引用到bean当中。

Spring基础12——使用外部属性文件的更多相关文章

  1. 使用Spring IOC容器引用外部属性文件

    一.引用外部属性文件 1.编写属性文件,以键值对形式存储,并放置在类路径(src)下 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/BOOKSTORE?rewrit ...

  2. Spring基础—— 在 Spring Config 中使用外部属性文件

    一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...

  3. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  4. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. spring 使用外部属性文件

    一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...

  6. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  7. Spring 使用外部属性文件配置

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  8. 十八 Spring的JDBC模板:引入外部属性文件

    配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...

  9. spring4-2-bean配置-6-使用外部属性文件

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk0AAAFGCAIAAAD4tzxRAAAgAElEQVR4nO2d27HsOm+tOxWn4CeXAm ...

随机推荐

  1. BZOJ 4422 Cow Confinement (线段树、DP、扫描线、差分)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=4422 我真服了..这题我能调一天半,最后还是对拍拍出来的...脑子还是有病啊 题解: ...

  2. Windows10系统内置Linux

    主要是在等电脑安装系统,有点慢,于是写个博客…… 还是那句话,从今年开始NOIP应该就不让用Windows了,所以还是尽早转Linux吧,不然NOIP考场上不会编译太尴尬对吧. 在学校电脑有Linux ...

  3. redis的安装及使用总结

    Windows版本的安装 下载地址:https://www.jb51.net/softs/541181.html 安装过程 把压缩包内的文件解压到非中文目录即可 启动redis 启动redis要通过命 ...

  4. OOM和SOF代码

    OutOfMemoryError大数组,例如图片加载. public class MockOutOfMemoryError { public static void main(String[] arg ...

  5. npm 错误记录

    npm run dev iview-weapp@1.1.0 dev /Users/Jovins/Desktop/小程序/iview-weapp gulp --gulpfile build/build- ...

  6. VLC2.2.4命令参数

    用法: vlc [选项] [流] ...您可以在命令行中指定多个流.它们将被加入播放列表队列.指定的首个项目将被首先播放. 选项样式: --选项 用于设置程序执行期间的全局选项. -选项 单字母版本的 ...

  7. leetcode-mid-design-297. Serialize and Deserialize Binary Tree¶-NO -??

    mycode 将list转换成树的时候没有思路 参考: deque 是双边队列(double-ended queue),具有队列和栈的性质,在 list 的基础上增加了移动.旋转和增删等 class ...

  8. easyhook源码分析三——申请钩子

    EasyHook 中申请钩子的原理介绍 函数原型 内部使用的函数,为给定的入口函数申请一个hook结构. 准备将目标函数的所有调用重定向到目标函数,但是尚未实施hook. EASYHOOK_NT_IN ...

  9. jest 的 coverage 提示 unknown 的解决方案

    概述 这几天玩 jest ,我在运行单元测试之后 coverage 总是显示 unknown,花了很多时间排查原因,最后终于想明白了,记录下来,供以后开发时参考,相信对其他人也有用. coverage ...

  10. Spring mvc注解说明

    编号 注解 说明 位置 备注 1 @Controller 将类变成Spring Bean 类 现阶段 @Controller . @Service 以及 @Repository 和 @Componen ...