1,配置数据源

(1)添加驱动

(2)编写spring配置文件

  1. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  2. <property name="username" value="scott"></property>
  3. <property name="password" value="scott"></property>
  4. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
  5. <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
  6. </bean>

(3)主方法

  1. package com.zsq;
  2.  
  3. import java.sql.SQLException;
  4. import javax.sql.DataSource;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7.  
  8. public class Main {
  9.  
  10. public static void main(String[] args) throws SQLException {
  11. // TODO Auto-generated method stub
  12. ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
  13. DataSource dataSource = (DataSource) ctx.getBean("dataSource");
  14. System.out.println(dataSource.getConnection());
  15. }
  16. }

结果:

这里已经证明可以正常连接到数据库。

2,存在问题

假如spring的配置文件里面有许多bean,这样文件大的话找到配置数据源的地方进行修改不方便。因此基本信息最好拿出来部署到属性文件里面最好。

•在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离

•Spring 提供了一个 PropertyPlaceholderConfigurer 的 BeanFactory 后置处理器, 这个处理器允许用户将 Bean 配置的部分内容外移到属性文件中. 可以在 Bean 配置文件里使用形式为 ${var} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.

•Spring 还允许在属性文件中使用 ${propName},以实现属性之间的相互引用。

配置步骤:

(1)添加属性文件---db.properties,填写如下内容,但是这些内容如何和spring里面的相关联呢?

(2)Bean 配置文件里使用形式为 ${var} 的变量, PropertyPlaceholderConfigurer 从属性文件里加载属性, 并使用这些属性来替换变量.

•Spring 2.5 之后: 可通过 <context:property-placeholder> 元素简化:

–<beans> 中添加 context Schema 定义

–在配置文件中加入如下配置:

  1. <!-- 导入属性文件 -->
  2. <context:property-placeholder location="classpath:db.properties"/>
  3. <!--使用外部文件属性 -->
  4. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  5. <property name="username" value="${username}"></property>
  6. <property name="password" value="${password}"></property>
  7. <property name="driverClassName" value="${driverClassName}"></property>
  8. <property name="url" value="${url}"></property>
  9. </bean>

这个时候会报错,仔细检查没啥错误啊,反正我在测试的时候找了很长时间的原因。

  1. Exception in thread "main" java.sql.SQLException: ORA-01017: invalid username/password; logon denied

后来我修改了配置文件和属性文件:

此时才解决问题:

Spring 学习笔记 8. 尚硅谷_佟刚_Spring_使用外部属性文件的更多相关文章

  1. Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean

    1,bean 的配置 <bean id="helloWorld" class="com.yfy.HelloWorld"> <property ...

  2. Spring学习笔记 5. 尚硅谷_佟刚_Spring_自动装配

    1,回顾以前的做法 一个人有姓名,有住址,有一辆车.其中住址和车也是一个类,这种情况下不用自动装配是十分容易实现的 (1)Person类 package com.zsq; public class P ...

  3. Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节

    1,字面值 •字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入. •基本数据类型及其封装类.String 等类型都可以采取字面值注入的方式 •若字 ...

  4. Spring学习笔记 1. 尚硅谷_佟刚_Spring_HelloWorld

    1,准备工作 (1)安装spring插件 搜索https://spring.io/tools/sts/all就可以下载最新的版本 下载之后不用解压,使用Eclipse进行安装.在菜单栏最右面的Help ...

  5. Spring 学习笔记 7. 尚硅谷_佟刚_Spring_Bean 的作用域

    1,理论 •在 Spring 中, 可以在 <bean> 元素的 scope 属性里设置 Bean 的作用域. •默认情况下, Spring 只为每个在 IOC 容器里声明的 Bean 创 ...

  6. Spring学习笔记 6. 尚硅谷_佟刚_Spring_Bean 之间的关系

    1,继承关系 首先从简单的代码来看,有一个Address类,配置文件有两个bean (1)Address类 package com.zsq; public class Address { privat ...

  7. Spring 学习笔记 2. 尚硅谷_佟刚_Spring_IOC&DI概述

    1,远古时代 这里讲述的IOC的演变历史,举一个例子,假如需要生成HTML和PDF格式的报表,以前的开发方式就是有个报表服务类需要使用报表生成器 它需要和其他三个都关联,它既需要知道接口类型,也需要知 ...

  8. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  9. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

随机推荐

  1. java写接口

    1.先导spring包 2.首先配置spring.xml的监听web.xml配置. <context-param> <param-name>contextConfigLocat ...

  2. .Net字符串替换

    在.Net中,有些地方需要进行字符的替换才能实现一些相关功能,这里是一个简单的字符串替换的方法 //如下,变量strWhere中是通过一些方法获取的sql拼接的条件语句,但在数据库中是多表查询,有同名 ...

  3. JQuery 刷新关闭页面

    关闭当前页面,刷新之前页面(js): window.opener.location.href = window.opener.location.href; window.close(); 刷新当前页面 ...

  4. Picture Segmentation Using A Recursive Region Splitting Method

    首先 区域分割的分割方法 首先得到一个区域,(一般已原图作为第一个区域) 然后得到该区域的直方图,选取最合适的峰值作为阈值,然后选择连接区域,然后把这些区域加入下一步要处理的区域中,然后重复第一步,直 ...

  5. [原创.数据可视化系列之二]使用cesium三维地图展示美国全球军事基地分布

    基于浏览器的三维地图还算是一个比较高冷的东西,最主要的技术难点是如何在浏览器上 多快好省 的显示三维数据,很遗憾,还真的没有太好的的方案,只能说还有可行的方案. 很久之前用过skyline,使用CS居 ...

  6. jquery设置自己的标识符

    $(function(){ var $jc=jQuery.noConflict(); $jc('.main').css({'margin':'0 auto'}) })

  7. hdu5442 Favorite Donut

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5442 题目大意:给你一个长度为n的字符串,将它首尾相连成环.问你这个环上找一个长度为n的字典序最大的串 ...

  8. ReactiveCocoa框架学习<一>

    1.Cocoapods导入ReactiveCocoa: use_frameworks! target 'RACDemo' do pod 'ReactiveObjC', '~> 2.1.0' en ...

  9. H5表单中placeholder属性的字体颜色问题

    最近做项目的时候遇到的一些小样式问题,有关表单.并且在接下来几天的面试人中五个人都没有回答上来,改变placeholder属性的默认字体颜色,感觉有必要总结一下. 如何改变默认字体的颜色? @blue ...

  10. 博弈论揭示了深度学习的未来(译自:Game Theory Reveals the Future of Deep Learning)

    Game Theory Reveals the Future of Deep Learning Carlos E. Perez Deep Learning Patterns, Methodology ...