Spring4.0学习笔记(4) —— 使用外部属性文件
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());
}
}
Spring4.0学习笔记(4) —— 使用外部属性文件的更多相关文章
- Spring学习记录(六)---使用外部属性文件
在bean配置资源或系统部署,如数据库的连接时,需要这样: 要包含相关jar包:c3p0.jar 和mysql.connector.jar xml配置: <bean id="dataS ...
- Java学习笔记——JDBC读取properties属性文件
Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- Spring4.0学习笔记(6) —— 通过工厂方法配置Bean
1.静态工厂方法: bean package com.spring.factory; public class Car { public Car(String brand) { this.brand ...
- Spring4.0学习笔记(5) —— 管理bean的生命周期
Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...
- Spring4.0学习笔记(3) —— Spring_Bean之间的关系
1.继承关系 bean-relation.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Spring4.0学习笔记(2) —— 自动装配
Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式 1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类 ...
- Spring4.0学习笔记(1) —— 基础知识
1.基本定义 IOC: 其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源,而应用了 IOC之后,容器主动将资源推送给它所管理的组件,组件索要做 ...
- Spring4.0学习笔记(11) —— Spring AspectJ 的五种通知
Spring AspectJ 一.基于注解的方式配置通知 1.额外引入的jar包: a) com.springsource.org.aopalliance-1.0.0.jar b) com.sprin ...
随机推荐
- 深度学习“引擎”之争:GPU加速还是专属神经网络芯片?
深度学习“引擎”之争:GPU加速还是专属神经网络芯片? 深度学习(Deep Learning)在这两年风靡全球,大数据和高性能计算平台的推动作用功不可没,可谓深度学习的“燃料”和“引擎”,GPU则是引 ...
- PartialFunction(偏函数)
val one:PartialFunction[Int,String]={ case 1 => "one" case 2 => "two" case ...
- ARP劫持攻击
今天下午,莫名其妙的产生. 部分客户的网站产生乱码,但本机访问或是好好的. 外网访问,乱码的原文件是一个<IFRAME>网页. 听说,有时ARP攻击是导致网络中断或时断时续. 安全狗和36 ...
- 调试makefile—subst函数
操作系统:ubuntu10.04 Makefile里的subst用法是$(subst FROM,TO,TEXT),即将TEXT中的东西从FROM变为TO Makefile中的字符串处理函数格式: ...
- 推送消息 相关公司 手机端分享http://mob.com/
信鸽 http://xg.qq.com/xg/pro/ctr_message 云巴 http://yunba.io/usercases/ 极光https://www.jpush.cn/ 手机端分享ht ...
- wxWidgets搜索事件处理函数顺序
详细参见:使用wxWidgets进行跨平台程序开发 (王强 译) 实例: class MyFrame:public wxFrame { public: MyFrame() { wxButton* bu ...
- has-a关系——多重私有继承
#ifndef _STUDENT_H_ #define _STUDENT_H_ #include <iostream> #include <string> #include & ...
- linux获取CPU温度
Centos系列 1 yum install lm_sensors 2 sensors-detect 3 sensors Ubuntu系列(多了service module-init-tools st ...
- A - 敌兵布阵 - hdu 1166
Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些 ...
- struts接收参数方式
第一种,直接用action的属性接收,是初学者常用的方法. package com.starain.user; public class User{ private String username; ...