先导,对IOC容器的理解

  • 通俗的讲就是把你的class类交给spring的IOC容器去管理
  • 需要对该类的属性注入一些值,就可以通过spring提供的xml文件或者注解进行注入
  • 自己使用时在IOC容器工厂中去获取就可以了,从而实现控制

1、导入maven依赖 5.3.15版本

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>pro01-maven-ider-parent</artifactId>
  7. <groupId>com.mhy.maven</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>pro06-module-spring</artifactId>
  12. <dependencies>
  13. <!-- https://mvnrepository.com/artifact/junit/junit -->
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>4.12</version>
  18. <scope>test</scope>
  19. </dependency>
  20. <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-webmvc</artifactId>
  24. <version>5.3.15</version>
  25. </dependency>
  26. </dependencies>
  27. </project>

2、xml配置文件的常用bean注入

  • 实体类
  1. public class Student {
  2. private String name;
  3. private Address address;
  4. private String[] books;
  5. private List<String> hobbys;
  6. private Map<String,String> card;
  7. private Set<String> games;
  8. private Properties info;
  9. private String wife;
  10. }
  • bean.xml的依赖注入
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="address" class="com.mhy.pojo.Address">
  7. <property name="address" value="重庆交通大学"/>
  8. </bean>
  9. <bean id="student" class="com.mhy.pojo.Student">
  10. <property name="name" value="mhy"/>
  11. <property name="address" ref="address"/>
  12. <property name="books">
  13. <array>
  14. <value>西游记</value>
  15. <value>红楼梦</value>
  16. <value>水浒传</value>
  17. <value>三国演义</value>
  18. </array>
  19. </property>
  20. <property name="hobbys">
  21. <list>
  22. <value>数学</value>
  23. <value>代码</value>
  24. </list>
  25. </property>
  26. <property name="games">
  27. <set>
  28. <value>LOL</value>
  29. <value>CF</value>
  30. <value>DMF</value>
  31. </set>
  32. </property>
  33. <property name="card">
  34. <map>
  35. <entry key="身份证" value="5002372222287878787"/>
  36. <entry key="学生卡" value="631910040417"/>
  37. </map>
  38. </property>
  39. <property name="info">
  40. <props>
  41. <prop key="driver">com.mysql.jdbc.Driver</prop>
  42. <prop key="url">url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8</prop>
  43. <prop key="username">root</prop>
  44. <prop key="password">123456</prop>
  45. </props>
  46. </property>
  47. <property name="wife">
  48. <null/>
  49. </property>
  50. </bean>
  51. </beans>
  • 测试的代码
  1. @Test
  2. public void testSpring2(){
  3. ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
  4. Student student = context.getBean("student", Student.class);
  5. System.out.println(student);
  6. /*结果--->
  7. Student{name='mhy',
  8. address=Address{address='重庆交通大学'},
  9. books=[西游记, 红楼梦, 水浒传, 三国演义],
  10. hobbys=[数学, 代码],
  11. card={身份证=5002372222287878787, 学生卡=631910040417},
  12. games=[LOL, CF, DMF],
  13. info={
  14. password=123456,
  15. url=url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8,
  16. driver=com.mysql.jdbc.Driver,
  17. username=root},
  18. wife='null'}
  19. */
  20. }
  • p和c标签的使用
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:c="http://www.springframework.org/schema/c"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. https://www.springframework.org/schema/beans/spring-beans.xsd">
  8. <bean id="hello" class="com.mhy.pojo.Hello">
  9. <property name="str" value="hello spring"/>
  10. </bean>
  11. <bean id="hello4" class="com.mhy.pojo.Hello">
  12. <property name="str" value="hello spring 2"/>
  13. </bean>
  14. <bean id="hello2" class="com.mhy.pojo.Hello" p:str="hello spring p"/>
  15. <bean id="hello3" class="com.mhy.pojo.Hello" c:str="hello spring c"/>
  16. </beans>

注意:

  1. c标签是set注入
  2. p标签是有参数构造注入

先导,对IOC容器的理解的更多相关文章

  1. Asp.Net Core 内置IOC容器的理解

    Asp.Net Core 内置IOC容器的理解 01.使用IOC容器的好处 对接口和实现类由原来的零散式管理,到现在的集中式管理. 对类和接口之间的关系,有多种注入模式(构造函数注入.属性注入等). ...

  2. 手写IOC容器

    IOC(控制翻转)是程序设计的一种思想,其本质就是上端对象不能直接依赖于下端对象,要是依赖的话就要通过抽象来依赖.这是什么意思呢?意思就是上端对象如BLL层中,需要调用下端对象的DAL层时不能直接调用 ...

  3. IOC容器模拟实现

    运用反射机制和自定义注解模拟实现IOC容器,使其具有自动加载.自动装配和根据全限定类名获取Bean的功能. 一. 实现原理 1-1 IOC容器的本质 IOC容器可理解为是一个map,其中的一个entr ...

  4. 深入理解DIP、IoC、DI以及IoC容器

    摘要 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念.通过本文我们将一起学 ...

  5. 基于nutz框架理解Ioc容器

    同样我们从问题入手去验证以及去理解Ioc容器都做了哪些事情: 1.nutz是有几种方式获取需要容器管理bean的信息? 第一种是使用json格式的文件进行配置,如: 第二种:使用注解@IocBean ...

  6. 深入理解DIP、IoC、DI以及IoC容器(转)

    深入理解DIP.IoC.DI以及IoC容器 摘要 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.D ...

  7. 【转】深入理解DIP、IoC、DI以及IoC容器

    原文链接:http://www.cnblogs.com/liuhaorain/p/3747470.html 前言 对于大部分小菜来说,当听到大牛们高谈DIP.IoC.DI以及IoC容器等名词时,有没有 ...

  8. 再看IOC, 读深入理解DIP、IoC、DI以及IoC容器

    IoC则是一种 软件设计模式,它告诉你应该如何做,来解除相互依赖模块的耦合.控制反转(IoC),它为相互依赖的组件提供抽象,将依赖(低层模块)对象的获得交给第三方(系统)来控制,即依赖对象不在被依赖模 ...

  9. IOC容器在web容器中初始化过程——(二)深入理解Listener方式装载IOC容器方式

    先来看一下ContextServletListener的代码 public class ContextLoaderListener extends ContextLoader implements S ...

随机推荐

  1. 992. Sort Array By Parity II - LeetCode

    Question 992. Sort Array By Parity II Solution 题目大意:给一个int数组,一半是奇数一半是偶数,分别对偶数数和奇数数排序并要求这个数本身是偶数要放在偶数 ...

  2. 有趣的BUG之Stack Overflow

    今天遇到一个很有意思的bug,当程序开发完成后打包到服务器运行,总是会出现栈溢出异常,经过排查发现,问题出现在一个接口上,但这个接口逻辑并不复杂,除了几局逻辑代码外和打印语句之外也没有其他的了,但是只 ...

  3. einsum函数介绍-张量常用操作

    einsum函数说明 pytorch文档说明:\(torch.einsum(equation, **operands)\) 使用基于爱因斯坦求和约定的符号,将输入operands的元素沿指定的维数求和 ...

  4. PyTorch的Variable已经不需要用了!!!

    转载自:https://blog.csdn.net/rambo_csdn_123/article/details/119056123 Pytorch的torch.autograd.Variable今天 ...

  5. CAP 6.1 版本发布通告

    前言 今天,我们很高兴宣布 CAP 发布 6.1 版本正式版,在这个版本中我们主要针对目前已经发现的几个BUG进行了修复了以及添加了一些小特性. 那么,接下来我们具体看一下吧. 总览 可能有些人还不知 ...

  6. Ubuntu安装python固定版本

    一. 安装python3.7 本篇文章使用python3.7安装步骤为例 1.直接使用apt-get安装python3.7 apt-get install python3.7 该方法经常会出现unab ...

  7. .NET Core 企业微信回调配置

    1.配置API接收 2.下载加密解密库 地址:https://developer.work.weixin.qq.com/devtool/introduce?id=36388,也可以复制下面的代码 2. ...

  8. 龙芯发布 .NET 6 SDK 6.0.105-ea1 LoongArch64 版本

    龙芯平台.NET,是龙芯公司基于开源社区.NET独立研发适配的龙芯版本,我们会长期进行安全更新和错误修复,并持续进行性能优化.社区.NET7版本开始已经原生支持LoongArch64架构源码.具备如下 ...

  9. vue大型电商项目尚品汇(后台篇)day03

    今天把平台属性的管理基本完成了,后台管理做到现在基本也开始熟悉,确实就是对ElementUI的一个熟练程度. 一.平台属性管理 1.动态展示数据 先把接口弄好,应该在第三级标题选择后进行发请求 静态页 ...

  10. gulp入门第一课你需要注意的

    安装 1.首先确保你已经正确安装了nodejs环境.然后以全局方式安装gulp. npm install -g gulp 2.初始化项目. npm init 3.如果想在安装的时候把gulp写进项目p ...