IOC操作Bean管理(外部属性文件)

1.直接配置数据库信息

(1)配置德鲁伊druid连接池

(2)引入德鲁伊druid连接池依赖jar包

 <!--直接配置连接池--><bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value=""></property><property name="url" value=""></property><property name="username" value=""></property><property name="password" value=""></property></bean>

填入数据库信息:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--直接配置连接池--><bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/db3"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean></beans>

2.引入外部属性文件配置数据库连接池

(1)创建外部属性文件,properties格式文件,写数据库信息

prop.driverClass=com.mysql.cj.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/db3
prop.userName=root
prop.password=8888.216

(2)把外部properties属性文件引入到spring配置文件中

a.引入context名称空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"></beans>

b.在spring配置文件使用标签引入外部属性文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--    &lt;!&ndash;直接配置连接池&ndash;&gt;-->
<!--    <bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource">-->
<!--        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>-->
<!--        <property name="url" value="jdbc:mysql://localhost:3306/db3"></property>-->
<!--        <property name="username" value="root"></property>-->
<!--        <property name="password" value="8888.216"></property>-->
<!--    </bean>--><!--引入外部属性文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--配置连接池--><bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${prop.driverClass}"></property><property name="url" value="${prop.url}"></property><property name="username" value="${prop.userName}"></property><property name="password" value="${prop.password}"></property></bean></beans>

[Spring5]IOC容器_Bean管理XML方式_外部属性文件相关推荐

  1. [Spring5]IOC容器_Bean管理XML方式_创建对象_set注入属性and有参构造注入属性

    IOC操作 Bean管理 什么是Bean管理 1.Bean管理指的是两个操作: a.Spring创建对象 b.Spring注入属性 2.Bean管理操作有两种方式 a.基于xml配置文件方式实现 b. ...

  2. [Spring5]IOC容器_Bean管理XML方式_自动装配

    IOC操作Bean管理(xml自动装配) package com.atguigu.spring.autowire;public class Dept {@Overridepublic String t ...

  3. [Spring5]IOC容器_Bean管理XML方式_注入集合类型属性

    xml注入集合属性 1.注入数组类型属性 2.注入List集合类型属性 3.注入Map集合类型属性 (1)创建类,定义数组,list,map,set类型属性,生成对应set方法 package com ...

  4. [Spring5]IOC容器_Bean管理XML方式_注入其他类型属性

    xml注入其他属性 bean: package com.atguigu.spring;/*** 演示使用set方法进行注入属性*/ public class Book {private String ...

  5. [Spring5]IOC容器_Bean管理注解方式_注入属性@Autowired_@Qualified_@Resource_@Value

    基于注解方式实现属性注入 (1)@AutoWired:根据属性类型进行自动装配 第一步 把service和dao对象创建,在service和dao类添加创建对象注解 第二步 在service注入dao ...

  6. IOC操作Bean管理XML方式(外部属性文件)

    目录 IOC操作Bean管理XML方式(外部属性文件) 前情引入: 实验演示: 1.直接配置数据库信息 (1)配置德鲁伊连接池 (2)引入德鲁伊连接池jar包 (3)创建一个bean6.xml配置文件 ...

  7. [Spring5]IOC容器_Bean管理注解方式_创建对象

    IOC操心Bean管理(基于注解方式) 1.什么是注解 (1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值-) (2)使用注解,注解作用在类上面,方法上面,属性上面 (3) ...

  8. [Spring5]IOC容器_Bean管理XML方式_p名称空间注入

    iii.第三种注入方式:p名称空间注入 bean: package com.atguigu.spring;/*** 演示使用set方法进行注入属性*/ public class Book {priva ...

  9. [Spring5]IOC容器_Bean管理注解方式_完全注解开发

    完全注解开发 (1)创建配置类,替代xml配置文件 package com.atguigu.spring.config;import org.springframework.context.annot ...

最新文章

  1. linux与windos 设置 tomcat 内存
  2. 设计模式——装饰者(Decorator)模式DEMO——游戏角色的装饰者模式实现
  3. 软件设计模式—控制反转
  4. 深入理解Struts2
  5. python3-开发进阶-仿博客园项目setting.py的文件的配置,admin,forms(2)
  6. P2150-[NOI2015]寿司晚宴【dp】
  7. 干货 | 局部特征图像配准用于缺陷检测
  8. JWT token信息保存
  9. 4种实例 advice aop_JAVA动态代理 和 Spring AOP 4种通知的简单实现
  10. JSON数据格式转换(StringBuffer拼接json串)大全及用法
  11. 计算机设置启动恢复出厂设置密码,bios怎么恢复出厂设置方法
  12. C# 如何添加PPT背景(纯色背景、渐变色背景、图片背景)
  13. 微信表情包小程序,更新登录接口,增加举牌功能
  14. matlab 生成hasse图,Hasse图详解
  15. php linux OpenOffice+JODConverter+php实现将word/ppt/excel文档转换为pdf
  16. 大数据、java、python、区块链、人工智能发展前景
  17. 威尔克姆绣花软件wilcome2.0t怎么安装教程?及解压密码获取
  18. C++模板类的运算符重载
  19. 西方哲学史 -- 阿那克西曼德
  20. TCP流量控制与拥塞控制原理分析

热门文章

  1. Hadoop的伪分布安装 hadoop的核心思想
  2. ajax常见错误和使用总结
  3. php 计时器microtime 以及去掉数组重复值array_unique
  4. db2和mysql语句区别_db2和mysql语法的区别是什么
  5. mysql关系数据库引擎_MySQL数据库引擎详解
  6. matlab求kcf算法响应图_Kernelized Correlation Filters(KCF)算法
  7. sublime编辑python_在没有安装Python的前提下,让Sublime text编辑器来运行Py?
  8. 用MATLAB三步完成机器人搭建
  9. 复工之后,如何让自己的时间更值钱
  10. 快速入门深度学习,其实并不难!