Spring4.0学习笔记(4) —— 使用外部属性文件
2024-08-24 01:07:17
1、配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <context:property-placeholder location="classpath:db.properties"/> <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>
2、外部配置文件
user=root
password=admin
driverclass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///authority
3、main方法
package com.spring.properties; import java.sql.SQLException; import javax.sql.DataSource; import org.springframework.context.*;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-properties.xml"); DataSource dataSource = (DataSource)ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
}
最新文章
- N皇后问题
- .Net中的加密解密
- git之一
- Liferay 6.2 改造系列之七:关闭使用条款确认、密码提醒、新用户强制修改密码等功能
- 微信网页授权,微信登录,oauth2
- Ajax实现联动效果
- Ceph的客户端安装
- 深入理解Android内存管理原理(六)
- linux就是这个范儿之融于心而表于行(1)
- dtrace4linux_Example
- EF搭建可扩展菜单
- 【原创】Java基础之Session机制
- 转 freemarker macro(宏)的使用
- centos7更改网卡名称
- Hanlp1.7版本的新增功能一览
- JavaScript 模拟 HashMap例子
- 14. Longest Common Prefix(暴力循环)
- 几个方便的nodejs 任务调度框架
- oracle A用户访问B用户的表aa
- Matlab的集合运算[转]
热门文章
- Remember the Word
- C++编程中对缓冲区的理解(OS默认4096大小的缓冲区,有例子,很形象)
- moveToThread的最简单用法(依葫芦画瓢即可)(使得线程也更偏向于信号槽的使用方法)
- SVN项目库错误Unsupported FS format svn: Expected FS format between &#39;1&#39; and &#39;4&#39;; found format &#39;6&#39;
- Event — Windows API
- Swift基础语法学习总结一
- ubunu设置java命令为全局的命令-添加到全局环境变量
- C++ —— 非常量引用不能指向临时对象
- motan源码分析八:涉及到底层的客户端调用
- Oracle&;#39;s Business Intelligence Applications Configuration Manager 基本概念