【跟我学spring 4.0 】之第七节-spring使用外部属性文件-spring配置连接oracle数据库


使用外部属性文件

         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 运行结果:




好啦,终于写完了,这期就到这里了。大冬天的,好冷啊,手都冻僵了~~~呜呜~~~~

=========================================================================================================================================================================================================================================================================================






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值