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. 3D Computer Grapihcs Using OpenGL - 17 添加相机(旋转)

    在11节我们说过,MVP矩阵中目前只应用了两个矩阵,World to View 矩阵被省略了,这就导致我们的画面没有办法转换视角. 本节我们将添加这一环节,让相机可以旋转. 为了实现这一目的,我们添加 ...

  2. 采用.bat批处理命令快速设置Java环境变量

    背景: java课程培训,每次到机房需要重新安装JDK,每次都采用图形界面进行操作比较麻烦(慢),于是在网上查了一下CMD命令设置系统环境变量的方法,再次记录下来. 设置方法: 1.找到JDK安装路径 ...

  3. 让DOM元素自动滚到视野内ScrollIntoView

    概述 项目中需要把一个DOM元素自动滚动到视野内,百思不得其解,最后再element库里面发现了这个方法,记录下来供以后开发时参考,相信对其他人也有用. 参考资料:element scroll-int ...

  4. python学习笔记:(五)列表与元组的异同

    在python中最基本的数据结构是序列(sequence),每一个元素被分配一个序号,即元素的位置,也称为索引,第一个索引是0,第二个则是1 元组与列表最大的区别就是: 元组不能更改:列表可以修改 p ...

  5. 批处理文件将多台连接的手机安装同一个APP

    :adb devices > C:\Users\aaminaxu\Desktop\a.txt :1.首先获取连接到电脑的手机的设备信息将其保存到C盘的a.txt文件中::2.将保存的txt文件中 ...

  6. cocos2dx基础篇(18) 数据存储CCUserDefault

    在cocos2dx中提供了一个数据存储类CCUserDefault,可以作为一个轻量级的数据库来使用.它支持五种数据bool.int.float.double.string的存储. [3.x]     ...

  7. Java多线程学习——join方法的使用

    join在线程里面意味着“插队”,哪个线程调用join代表哪个线程插队先执行——但是插谁的队是有讲究了,不是说你可以插到队头去做第一个吃螃蟹的人,而是插到在当前运行线程的前面,比如系统目前运行线程A, ...

  8. TCP/IP协议-1

    转载资源,链接地址https://www.cnblogs.com/evablogs/p/6709707.html

  9. 简述Vue项目中返回上一页

    1.背景 由于Vue所生成的项目叫做单页应用,即SPA,如果还是使用jQuery中的go(-)或back()是行不通的,所以,我们使用到了Vue中的编程式导航. 2.基本使用 定义返回按钮: < ...

  10. android开发错误经验总结

    TextView: 1.textView.setText();参数如果直接传int类型,ide不会显示错误.但是运行会报错. 布局渲染: 1. <View android:background= ...