先导,对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. 关于我开发tinymce的自由表单、病历插件这件事

    项目地址:https://gitee.com/zhao-xuhang/tinymce 1.前期准备 这是个vue2项目所以要使用vue-cli (虽然开发tinymce插件和这个没关系) 1. 使用n ...

  2. babel使用

    Babel转码器 Babel定义 Babel 是一个广泛使用的 ES6 转码器,可以将 ES6 代码转为 ES5 代码,从而在老版本的浏览器执行 Babel安装 仅需要在项目文件下安装 npm ins ...

  3. 第06组 Beta冲刺 (2/5)

    目录 1.1 基本情况 1.2 冲刺概况汇报 1.郝雷明 2. 方梓涵 3.杜筱 4.黄少丹 5. 董翔云 6.鲍凌函 7.詹鑫冰 8.曹兰英 9.曾丽莉 10.吴沅静 1.3 冲刺成果展示 1.1 ...

  4. redis+lua实现脚本一键查询

    场景 经常需要查redis某个key的值,需要执行三条命令才能查到 redis-cli,启动redis select num,选择db get key,查询语句 需要执行三条命令才能实现某个key的查 ...

  5. pytorch基础常识

     

  6. 蓝牙、WiFi、ZigBee三大无线通信技术协议模块哪一个是最好的?

    曾经,在2015年极客公园创新大会上,小米首次在非官方平台发布了新款产品小米智能家庭套装.自此,Zigbee便常出现在大众视野中. 如今,小米在IoT物联网应用开发者平台上明确说明,不再推广Zigbe ...

  7. 为什么 C# 访问 null 字段会抛异常?

    一:背景 1. 一个有趣的话题 最近在看 硬件异常 相关知识,发现一个有意思的空引用异常问题,拿出来和大家分享一下,为了方便讲述,先上一段有问题的代码. namespace ConsoleApp2 { ...

  8. SAP 实例 10 List Box with value list from input help

    *&---------------------------------------------------------------------* *& Report DEMO_DROP ...

  9. SAP 隐式增强 Enhancement point

    1.进入编辑器:SE38/SE37/SE24 Edit-->Enhancement Operations-->Create Option 2.填写相关信息,点击对号. 3.点击Enhanc ...

  10. Freeswitch使用originate转dialplan

    概述 Freeswitch是一款非常好用的开源VOIP软交换平台. 最近在对fs做一些功能测试,测试的过程中产生的一个需求,如何从fs发起呼叫并把后续的呼叫流程转到某一个dialplan上,这样在测试 ...