网站建设专员招聘,北京网站改版报价,青岛黄岛区建设工程管理局网站,商城网站前台模板免费下载目录
IOC操作Bean管理XML方式#xff08;外部属性文件#xff09;
前情引入#xff1a;
实验演示#xff1a; 1.直接配置数据库信息
#xff08;1#xff09;配置德鲁伊连接池
#xff08;2#xff09;引入德鲁伊连接池jar包
#xff08;3#xff09;创建一个b…目录
IOC操作Bean管理XML方式外部属性文件
前情引入
实验演示 1.直接配置数据库信息
1配置德鲁伊连接池
2引入德鲁伊连接池jar包
3创建一个bean6.xml配置文件 2.引入外部属性文件配置数据库连接池
1创建外部属性文件 properties 格式的配置文件写数据库信息
步骤一创建文件 jdbc.properties 用来写数据库配置
2把外部 properties 属性文件引入到 Spring 配置文件中
步骤一引入 context 名称空间
步骤二在 spring 配置文件使用标签引入外部属性文件 IOC操作Bean管理XML方式外部属性文件 前情引入
问题之前的做法是创建一个类在bean中进行属性注入 但是如果一个类中属性很多就需要写很多propertyproperty/
这么写其实不是很方便如果属性值发生一些变化的话还需要去bean.xml配置文件中进行修改 解决将一些 固定的值 或者 相关的值 放到一个其它的文件中
场景应用比如操作数据库中有些数据库的固定值例如数据库的地址、用户名 、密码......就可以把数据库这些值放到一个property的配置文件中然后再在 xml配置文件中去读取property配置文件中的内容就是这么一个注入过程 实验演示
1.直接配置数据库信息
1配置德鲁伊连接池
2引入德鲁伊连接池jar包 什么是德鲁伊连接池jar包Druid德鲁伊是阿里巴巴开发的号称为监控而生的数据库连接池Druid是目前最好的数据库连接池。在功能、性能、扩展性方面都超过其他数据库连接池同时加入了日志监控可以很好的监控DB池连接和SQL的执行情况。Druid已经在阿里巴巴部署了超过600个应用经过一年多生产环境大规模部署的严苛考验 首先复制druid-1.0.9.jar包到项目中
1 2 3 4 5 3创建一个bean6.xml配置文件
直接配置 连接池
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd!--最基本的直接配置连接池--
bean iddataSource classcom.alibaba.druid.pool.DruidDataSource!--代表驱动名称--property namedriverClassName valuecom.mysql.jdbc.Driver/property!--数据库的地址 jdbc:mysql://localhost:3306 名字 userDb--property nameurl valuejdbc:mysql://localhost:3306/userDb/property!--登录用户名和密码--property nameusername valueroot/propertyproperty namepassword valueroot/property
/bean/beans 查看源码
数据库名称以及驱动的名字......这些名字大部分是固定的 问题配置好的值现在是固定的并且是直接在xml文件里面手写出来不是引入 解决现在把值写入外部的配置文件中然后把配置文件引入进来因此我们下面的操作就是 引入外部属性文件配置数据库连接池 2.引入外部属性文件配置数据库连接池 【说白了就是这么引入好维护容易扩展】
1创建外部属性文件 properties 格式的配置文件写数据库信息
步骤一创建文件 jdbc.properties 用来写数据库配置 jdbc.properties 代码如下 .properties内容都是键值形式 prop.driverClasscom.mysql.jdbc.Driver
prop.urljdbc:mysql://localhost:3306/userDb
prop.userNameroot
prop.passwordroot2把外部 properties 属性文件引入到 Spring 配置文件中 步骤一引入 context 名称空间 步骤二在 spring 配置文件使用标签引入外部属性文件 就可以通过一个名称去引用一个文件这样做比较 好维护
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd!--从本地location 引入外部属性文件--context:property-placeholder locationclasspath:jdbc.properties/!--配置连接池--bean iddataSource classcom.alibaba.druid.pool.DruidDataSource!--不是固定值代表驱动名称 通过表示式从文件中--property namedriverClassName value{prop.driverClass}/property!--不是固定值数据库的地址 jdbc:mysql://localhost:3306 名字 userDb--property nameurl value{prop.url}/property!--不是固定值登录用户名和密码--property nameusername value{prop.userName}/propertyproperty namepassword value{prop.password}/property/bean/beans