• spring2:IOC思想和DI思想(基于xml)


    一.思想简介

    1.IOC

    • 反转控制:即直接使用IOC容器提供的对象(将对象控制权交由框架管理)

    2.DI

    • 依赖注入:即对IOC容器提供的对象的属性进行赋值

    二.使用

    1.整体框架

    在这里插入图片描述

    2.获取容器提供的对象

    ①整体流程

    • 根据xml配置文件找到对应id的类(Class.forName)
      本质为反射,编译时可以不确定类类型
    • 将类的实例化对象和id注入到IOC容器中(.newInstance)
      本质为调用无参构造器
    • 在调用方法获取对象时对比id或者类型即可

    ②根据bean的id获取对象

    • 通过对比id获取容器中的对象
    • 缺点:返回值为Object类型
    • 例子
    <bean id="helloworld" class="com.atguigu.spring.bean.HelloWorld">bean>
    
    public void testHelloWorld(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloWorld helloworld = (HelloWorld) ac.getBean("helloworld");
    helloworld.sayHello();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ③根据bean的类型获取对象

    • 通过对比类型(instanceof)来获取对象,返回值类型为对象的类型
    • 优点:可以通过继承的类,实现的接口来寻找对象
    • 缺点:同类型查找,只能有一个匹配的对象
    • 例子:
    @Test
    public void testHelloWorld(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloWorld bean = ac.getBean(HelloWorld.class);
    bean.sayHello();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ④根据id和类型获取对象

    • 例子:
    @Test
    public void testHelloWorld(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloWorld bean = ac.getBean("helloworld", HelloWorld.class);
    bean.sayHello();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.DI的依赖注入

    ①set注入

    <bean id="studentOne" class="com.atguigu.spring.bean.Student">
    
    
    
    <property name="id" value="1001">property>
    <property name="name" value="张三">property>
    <property name="age" value="23">property>
    <property name="sex" value="">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ②构造器注入

    <bean id="studentTwo" class="com.atguigu.spring.bean.Student">
    <constructor-arg value="1002">constructor-arg>
    <constructor-arg value="李四">constructor-arg>
    <constructor-arg value="33">constructor-arg>
    <constructor-arg value="">constructor-arg>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ③p命名空间注入

    <bean id="studentSix" class="com.atguigu.spring.bean.Student"
    p:id="1006" p:name="小明" p:clazz-ref="clazzOne" p:teacherMapref="teacherMap">bean>
    
    • 1
    • 2

    4.DI依赖注入的类型

    ①字面量

    • 直接使用 即可
    • 其中特殊值null
    <property name="name">
    <null />
    property>
    
    • 1
    • 2
    • 3
    • 其中特殊符号<>
    
    
    <property name="expression" value="a < b"/>
    <property name="expression">
    
    
    
    
    <value>value>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ②类类型赋值

    • 引用已声明的类
    <property name="clazz" ref="clazzOne">property>
    
    • 1
    • 引用内部bean
    <property name="clazz">
    
    
    <bean id="clazzInner" class="com.atguigu.spring.bean.Clazz">
    <property name="clazzId" value="2222">property>
    <property name="clazzName" value="远大前程班">property>
    bean>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 级联属性赋值(使用少)
    <bean id="studentFour" class="com.atguigu.spring.bean.Student">
    <property name="id" value="1004">property>
    <property name="name" value="赵六">property>
    <property name="age" value="26">property>
    <property name="sex" value="">property>
    
    <property name="clazz" ref="clazzOne">property>
    <property name="clazz.clazzId" value="3333">property>
    <property name="clazz.clazzName" value="最强王者班">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ③数组类型赋值

    • 内部array
    <property name="hobbies">
    <array>
    <value>抽烟value>
    <value>喝酒value>
    <value>烫头value>
    array>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 引用array数组的bean

    ④集合类型list赋值

    • 内部list
    <property name="students">
    <list>
    <ref bean="studentOne">ref>
    <ref bean="studentTwo">ref>
    <ref bean="studentThree">ref>
    list>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 引用list集合的bean
    <util:list id="studentlist">
    <ref bean="studentOne">ref>
    <ref bean="studentTwo">ref>
    <ref bean="studentThree">ref>
    util>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ⑤集合类型map赋值

    • 内部map
    <property name="teacherMap">
    <map>
    <entry>
    <key>
    <value>10010value>
    key>
    <ref bean="teacherOne">ref>
    entry>
    <entry>
    <key>
    <value>10086value>
    key>
    <ref bean="teacherTwo">ref>
    entry>
    map>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 引用map类型的bean
    <util:map id="teacherMap">
    <entry>
    <key>
    <value>10010value>
    key>
    <ref bean="teacherOne">ref>
    entry>
    <entry>
    <key>
    <value>10086value>
    key>
    <ref bean="teacherTwo">ref>
    entry>
    util:map>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    【Linux升级之路】8_Linux多线程
    npm yarn 一起使用报错
    LeetCode 刷题 [C++] 第73题.矩阵置零
    <C++>多继承以及典型的菱形继承案例
    代码随想录算法训练营day57 | 647. 回文子串,516.最长回文子序列
    docker 常用命令
    Python_scrapy(知乎问答爬取
    MySQL数据库:7、SQL常用查询语句
    9.21号作业
    Codeforces Round 894 (Div. 3) E. Kolya and Movie Theatre
  • 原文地址:https://blog.csdn.net/qq_44724899/article/details/127686588