先导,对IOC容器的理解

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

1、导入maven依赖 5.3.15版本

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pro01-maven-ider-parent</artifactId>
<groupId>com.mhy.maven</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>pro06-module-spring</artifactId> <dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency> </dependencies> </project>

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

  • 实体类
public class Student {

    private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;
}
  • bean.xml的依赖注入
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.mhy.pojo.Address">
<property name="address" value="重庆交通大学"/>
</bean>
<bean id="student" class="com.mhy.pojo.Student"> <property name="name" value="mhy"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<property name="hobbys">
<list>
<value>数学</value>
<value>代码</value>
</list>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>CF</value>
<value>DMF</value>
</set>
</property>
<property name="card">
<map>
<entry key="身份证" value="5002372222287878787"/>
<entry key="学生卡" value="631910040417"/>
</map>
</property>
<property name="info">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
<property name="wife">
<null/>
</property> </bean> </beans>
  • 测试的代码
    @Test
public void testSpring2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student);
/*结果--->
Student{name='mhy',
address=Address{address='重庆交通大学'},
books=[西游记, 红楼梦, 水浒传, 三国演义],
hobbys=[数学, 代码],
card={身份证=5002372222287878787, 学生卡=631910040417},
games=[LOL, CF, DMF],
info={
password=123456,
url=url=jdbc:mysql://localhost:3306/smbms?characterEncoding=utf8,
driver=com.mysql.jdbc.Driver,
username=root},
wife='null'}
*/
}
  • p和c标签的使用
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.mhy.pojo.Hello">
<property name="str" value="hello spring"/>
</bean>
<bean id="hello4" class="com.mhy.pojo.Hello">
<property name="str" value="hello spring 2"/>
</bean>
<bean id="hello2" class="com.mhy.pojo.Hello" p:str="hello spring p"/>
<bean id="hello3" class="com.mhy.pojo.Hello" c:str="hello spring c"/> </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. 好客租房40-react组件基础综合案例-案例需求分析

    实现 案例的数据 渲染评论列表 有评论 没有评论 暂无评论 获取评论信息 包括评论人和受控组件 发表评论 更新评论 //导入react import React from 'react' import ...

  2. 135_Power Query M语言快捷输入之输入法设置自定义短语

    博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 因为工作原因,把电脑重装了下,当敲M的时候总感觉那里不对.原来是我的M自定义短语没有同步.由于我的自定义短语还是 ...

  3. CefSharp 白屏问题

    原文 现象 我正在使用 cefsharp + winform 建立一个桌面程序用于显示网页.使用过程中程序会突然白屏,经过观察发现,在网页显示GIF动图时,浏览器子程序会突然占用较高内存(从80M上升 ...

  4. Nexus5x 刷机

    1.刷机方式 线刷 线刷的本质的是对分区的全部内容的替换,线刷的包通常比较大. 卡刷 顾名思义,将升级包放在存储卡上,然后进入Recovery引导模式对系统进行刷机.卡刷本质是对文件的替换过程.它不会 ...

  5. 封装axios请求

    import axios from 'axios' import router from '@/router' axios.defaults.baseURL = system.requestBaseU ...

  6. 【SNOI2017 DAY1】炸弹

    题意:P5024 思路:首先\(O(n^2)\)向能炸到的点连边,所以能到达的点的个数就是能到达的点的个数.然后显然要缩点+拓扑排序(我写的记搜). 然后再写一个线段树优化建图. 然后就WA了,我想了 ...

  7. SmartIDE v0.1.18 已经发布 - 助力阿里国产IDE OpenSumi 插件安装提速10倍、Dapr和Jupyter支持、CLI k8s支持

    SmartIDE v0.1.18 (cli build 3538) 已经发布,在过去的Sprint 18中,我们集中精力推进对 k8s 远程工作区 的支持,同时继续扩展SmartIDE对不同技术栈的支 ...

  8. 为什么Dapr是比SpringCloud和Istio更优雅的微服务框架?

    Dapr 是微软主导的云原生开源项目,2019年10月首次发布,到正式发布 V1.0 版本的不到一年的时间内,github star 数达到了 1.2万(现在已经超过1.7万星),超过同期的 kube ...

  9. 代码模板整理(0):先水一波a+b

    梦开始的地方 "Hello World!" #include<bits/stdc++.h> using namespace std; int main(){ cout& ...

  10. MySQL并行复制(MTS)原理(完整版)

    目录 MySQL 5.6并行复制架构 MySQL 5.7并行复制原理 Master 组提交(group commit) 支持并行复制的GTID slave LOGICAL_CLOCK(由order c ...