1,配置数据源

(1)添加驱动

(2)编写spring配置文件

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

(3)主方法

package com.zsq;

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
}
}

结果:

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

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 定义

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

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

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

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. ROS下创建第一个节点工程

    1.创建工作区 mkdir catkin_ws cd catkin_ws mkdir src 2.在src目录下创建包Myrobot,后面所跟roscpp rospy为依赖包 catkin_creat ...

  2. eclipse中的 Compiler compliance level含义

    The compiler compliance setting tells the compiler to pretend it's a different version of Java. The ...

  3. The remote name could not be resolved问题的解决方法

    网站如果绑定了代理ip,内部跳转的时候,就会报The remote name could not be resolved错误,这个错误很难排查,网上也没有多少可参考的例子 现在记录下解决方法,以备参考 ...

  4. HTML5 --照抄书里的代码但函数无法执行、求分析( Uncaught ReferenceError: xxx is not defined)

    在js文件里写一个方法传参数: moveElement(id,name,price) { alert("id:"+id+"name:"+name+"p ...

  5. HTML实体符号代码速查表

    1.特色的 © © © 版权标志 |   | 竖线,常用作菜单或导航中的分隔符 · · · 圆点,有时被用来作为菜单分隔符 ↑ ↑ ↑ 上箭头,常用作网页“返回页面顶部”标识 € € € 欧元标识 ² ...

  6. Python3利用BeautifulSoup4批量抓取站点图片的代码

    边学边写代码,记录下来.这段代码用于批量抓取主站下所有子网页中符合特定尺寸要求的的图片文件,支持中断. 原理很简单:使用BeautifulSoup4分析网页,获取网页<a/>和<im ...

  7. geoserver使用curl发布 imagemosaic

    1.//create workspace     curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d ...

  8. mysql中的where和having子句的区别

    mysql中的where和having子句的区别 having的用法 having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句 ...

  9. [Linux] 结构化命令 if

    语法结构如下: 1. if-then语句 # if-then语句 if command #根据conmmand的退出状态码,选择执行语句 then commands fi e.g. #!usr/bin ...

  10. vps云服务器建站后绑定域名的方法?

    有很多的新手站长们,都不知道vps建站后该如何绑定自己的域名,这里就Windows系统的VPS主机利用iis绑定网站域名的方法,简要介绍一下. 通常情况下,我们在使用IIS建站的时候,都会有一步提示, ...