【跟我学spring 4.0 】之第七节-spring使用外部属性文件-spring配置连接oracle数据库
2015-12-29 00:13
761 查看
使用外部属性文件
1.在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(例如:文件路劲,数据源配置信息等)。而这些部署细节实际上需要和Bean配置相分离。2.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将bean配置的部分内容外移到属性文件中。可以在Bean配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性来替换变量。
3.Spring还允许在属性文件中使用${propName},以实现属性之间的相互引用。
实例:
注意:在部署之前,需要先将数据库安装好,这里我使用的是oracle数据库。在安装好oracle数据库之后,有一个jdbc.jar包文件,这个文件需要从本地安装到maven仓库中。(PS:我从http://mvnrepository.com/search?q=oracle网站仓库中直接引用,不知道为什么,eclipse表示下载不到!!)
2.1 jdbc手动安装到maven仓库中
在命令行中输入:(可以参考:在Maven仓库中添加Oracle JDBC驱动 http://www.linuxidc.com/Linux/2014-01/95933.htm 这篇文章)
mvn install:install-file -Dfile=D:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=10.2.0 -Dpackaging=jar
看到输出成功信息,那么就表示maven成功安装到我们本地maven仓库啦。
2.2 在pom.xml 引入C3P0 连接池、Jdbc
<!-- 引入spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- 引入c3p0连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- 引入oracle的jdbc --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>10.2.0</version> </dependency>
2.3 spring-properties.xml 配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName" default-lazy-init="true"> <!-- 导入属性文件 --> <context:property-placeholder location="classpath:db.propertites"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none"> <property name="driverClass"> <value>${jdbc.OracleDriver}</value> </property> <property name="jdbcUrl"> <value>${jdbc.url}</value> </property> <property name="user"> <value>${jdbc.user}</value> </property> <property name="password"> <value>${jdbc.password}</value> </property> </bean> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none"> <property name="driverClass"> <value>oracle.jdbc.driver.OracleDriver</value> </property> <property name="jdbcUrl"> <value>jdbc:oracle:thin:@localhost:1521:orcl</value> </property> <property name="user"> <value>zhengdong</value> </property> <property name="password"> <value>zhengdong</value> </property> </bean> </beans>
2.4 db.propertites配置文件:
jdbc.OracleDriver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl jdbc.user=zhengdong jdbc.password=zhengdong
2.5 主函数测试文件:
public class BeanPropertitesMainTest { public static void main(String[] args) throws Exception { ApplicationContext atx = new ClassPathXmlApplicationContext("spring-properties.xml"); DataSource dataSources = (DataSource) atx.getBean("dataSource"); System.out.println(dataSources); // Connection connection = dataSources.getConnection() ; Connection con = null;// 创建一个数据库连接 PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement ResultSet result = null;// 创建一个结果集对象 try { con =dataSources.getConnection() ; System.out.println("连接成功!"); String sql = "select sysdate as dd from dual ";// 预编译语句,“?”代表参数 pre = con.prepareStatement(sql);// 实例化预编译语句 result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数 while (result.next()) // 当结果集不为空时 System.out.println("数据库当前时间为:" + result.getString("dd") ); } catch (Exception e) { e.printStackTrace(); } finally { try { // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源 // 注意关闭的顺序,最后使用的最先关闭 if (result != null) result.close(); if (pre != null) pre.close(); if (con != null) con.close(); System.out.println("数据库连接已关闭!"); } catch (Exception e) { e.printStackTrace(); } } } }
2.6 控制台console 运行结果:
好啦,终于写完了,这期就到这里了。大冬天的,好冷啊,手都冻僵了~~~呜呜~~~~
=========================================================================================================================================================================================================================================================================================
相关文章推荐
- Oracle的oci和thin的不同
- oracle 11g如何完全卸载
- Oracle 11g R2安装手册(图文教程)For Windows
- Oracle VM VirtualBox 打开复制虚机报错
- ORACLE TO_CHAR函数格式化数字的出现空格的原因
- Oracle CASE WHEN 用法介绍
- oracle存储过程报"无效的列索引"的错误
- VS2015操作Oracle数据需要做那些设置?
- oracle 的那些事儿
- ORACLE 数据库的回滚要求
- Oracle EBS 交叉币种(3)-应收交叉币种
- RMAN实战16:ORACLE数据库的不完全恢复和闪回
- Oracle EBS 交叉币种(2)-应收交叉币种
- jdbc:oracle:thin:@192.168.3.98:1521:orcl(详解)
- 关于oracle 10g调度程序PLSQL_BLOCK方式无法使用truncate的问题
- Oracle EBS 交叉币种(1)-基础设置
- Oracle的四个主要用户
- 一次RAC环境下服务器故障重启后ORACLE启动过程
- 安装Oracle时可能碰到的常见问题-1
- Linux下Oracle重启问题