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. U-boot中的FDT

    1. U-boot为了支持FDT,添加了新的代码:/libfdt目录fdt.h libfdt.h fdt_support.h fdt_support.c 2. http://blog.csdn.net ...

  2. 检测Java程序运行时间的2种方法(高精度的时间[纳秒]与低精度的时间[毫秒])

    第一种是以毫秒为单位计算的. 代码如下: long startTime=System.currentTimeMillis(); //获取开始时间 doSomeThing(); //测试的代码段 lon ...

  3. @ModelAttribute 注解及 POJO入参过程

    一.modelattribute注解 @ModelAttribute注解的方法有两种,一种无返回值,一种有返回值,方法的可以用@RequestParam注解来获取请求的参数,如果不获取参数,可以不用此 ...

  4. sql server 2008 外键关联的设置和取消

    直接上图片 选中表右击-设计 找到需要设置外键的字段.右击-关系,在弹出的对话框中点击添加 选择右边的小按钮点击.选择主键表和关联的主键ID,以及外建表的关联字段. 建立外键完成. 删除的话选中某个外 ...

  5. 利用Unity制作“表”

    一枚小菜鸟   目前没发现在Unity有其他路径制作类似于c# WinForm中的表:但是利用Unity自带的UGUI,制作了一张"伪表",具体方案如下: 效果图如下: 步骤: 1 ...

  6. Android停止运行问题1_layout布局xml问题

    好好的写了300多行布局文件代码,写完之后运行结果,停止运行了.我当时就奇怪,xml有错误应该会提示啊,我就一个一个的缩小错误的代码范围,先直接换成一个简单的TextView ,运行一下没有错误.再慢 ...

  7. 第四十章 微服务CICD(2)- jenkins(war版)

    一.下载 官网下载war包,放在tomcat下的webapps下, 第一章 tomcat安装与启动 第二章 部署war包到tomcat jenkins:2.19.1版本. 二.修改编码为utf-8 在 ...

  8. kendoui treeview grid spreadsheet

    treeview 傻子方式获取id <!DOCTYPE html> <html> <head> <title>API</title> < ...

  9. 脚本录制(JMeter)

    使用JMeter录制脚本: 1.打开JMeter工具,创建一个线程组,接着创建一个http代理服务器,代理服务器设置如下:

  10. Nginx-->基础-->排错-->nginx错误总结

    一.启动时错误 1.错误提示: 2016/11/16 17:36:41 [emerg] 2458#2458: getpwnam("nginx") failed 查看错误日志文件内容 ...