spring学习笔记(7)——引用外部文件
来源:互联网 发布:sql 包含某几个字符 编辑:程序博客网 时间:2024/05/16 17:51
引用外部文件
我们先来模拟一个场景,假设我们现在要整合hibernate或者mybatis等框架到spring中
先写一个dataSource做模拟
package com.zj.ref;import java.util.Properties;public class DataSource { private String user; private String password; private String driverClass; private String jdbcUrl; public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverClass() { return driverClass; } public void setDriverClass(String driverClass) { this.driverClass = driverClass; } public String getJdbcUrl() { return jdbcUrl; } public void setJdbcUrl(String jdbcUrl) { this.jdbcUrl = jdbcUrl; } @Override public String toString() { return "DataSource [user=" + user + ", password=" + password + ", driverClass=" + driverClass + ", jdbcUrl=" + jdbcUrl + "]"; }}
然后spring配置文件中配置
<bean id="dataSource" class="com.zj.ref.DataSource"> <property name="user" value="root"></property> <property name="password" value="123"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///test"></property></bean>
写一个测试方法
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-ref.xml"); DataSource dataSource = (DataSource) ctx.getBean("dataSource"); System.out.println(dataSource); }
结果如下
配置没问题
但是在实际开发中,数据库的配置信息,我们一般都写到db.properties中,方便修改
在src下创建db.properties文件
spring配置(记得先加入context命名空间)
<?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-4.0.xsd"><context:property-placeholder location="classpath:db.properties"/><bean id="dataSource" class="com.zj.ref.DataSource"> <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>
- 使用
context:property-placeholder
声明数据库配置文件的位置- 使用${} 来读取配置文件中的属性值(类似EL表达式)
0 0
- spring学习笔记(7)——引用外部文件
- Spring 学习笔记(7)—— 使用外部属性文件
- 12.笔记JAVA Spring框架学习————Bean使用外部属性文件
- spring学习笔记——摘录:对properties文件引用的两种方法
- Spring引用外部文件生成主键配置
- Spring配置中引用外部属性文件
- spring配置文件location引用外部文件
- spring 5.0 引用外部文件 03
- Spring学习笔记之使用外部属性文件
- spring学习笔记六 使用外部属性文件
- Angular学习(十)——表达式、外部src引用
- JavaScrip学习之旅(2)引用外部JS文件
- 8.Spring学习笔记_使用外部属性文件(by尚硅谷_佟刚)
- #学习笔记#(2)引用外部CSS
- spring笔记——加载外部资源
- 学习笔记——c++primer学习(二)_头文件引用
- Extjs4.0学习笔记(文件引用)
- Extjs4.0学习笔记(文件引用)
- LeetCode : No12 Integer to Roman
- Ubuntu su 认证失败 解决办法
- Intent详解
- swift tableview中添加侧滑删除功能 类似qq删除
- codebook 背景建模更新及码本模型如何释放内存
- spring学习笔记(7)——引用外部文件
- 翻页控件 刷新
- Android开发秘籍学习笔记(一)
- android - How to make a phone call from webview
- AsyncTask和Handler对比
- android 端极光推送集成,适用于webview
- Android框架设计总结
- 数字证书原理
- Hibernate中数据库无法插入中文记录的解决方法