六properties属性文件的使用
来源:互联网 发布:大数据分析可视化 编辑:程序博客网 时间:2024/06/15 00:23
在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离 。
Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.
ex:
首先导入c3p0和mysqlconnector的jar包:
新建一个配置文件:
user=rootpassword=1230driverclass=com.mysql.jdbc.Driverjdbcurl=jdbc:mysql:///test再创建xml文件:
<?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/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--step1--> <context:property-placeholder location="db.properties"/> <!--step2--> <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="driverClass" value="${driverclass}"></property> <property name="jdbcUrl" value="${jdbcurl}"></property> </bean></beans>
public static void main(String[] args){ ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource= (DataSource) ctx.getBean("datasource"); System.out.println(dataSource); }
Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用。
整个项目的地址https://coding.net/u/littleWhiteJ/p/SpringStudy
0 0
- 六properties属性文件的使用
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SEAPI读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SE API读properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SEAPI读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- 使用J2SEAPI读取Properties文件的六种方法
- 使用J2SE API读取Properties文件的六种方法
- Hadoop集群SSH服务和免密码登录的配置
- QT make时出现的错误 NO SUCH FILE
- SpringBoot学习笔记二(常规属性配置,类型安全配置,日志配置,Profile配置)
- Learning Spark笔记1-Spark介绍
- 邮件服务器搭建——linux中PowerMTA+OEMPRO安装与配置方案
- 六properties属性文件的使用
- ntko打开文件,区分图片和office
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(七)图片上传功能
- shell脚本编程入门基础(附:自动备份与解包文件脚本)
- SQL SERVER 2008数据库的表中修改字段的数据类型后,不能保存
- 数据库中的触发器的实例讲解
- 基本样式
- span和图片
- Turbine怎样获取到节点的监控信息