• Spring6学习技术|IoC+基于xml管理bean


    学习材料

    尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)

    IoC

    控制反转。是一种设计思想。
    在这里插入图片描述

    1.获取bean对象的方法

    通过id,通过class,和双重方式。

            ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
            User user = (User) context.getBean("user");
            System.out.println("通过id获取bean对象:"+user);
    
            User user2 = context.getBean(User.class);
            System.out.println("通过类型获取bean对象:"+user2);
    
            User user3 = context.getBean("user", User.class);
            System.out.println("通过id和类型获取bean对象:"+user3);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.依赖注入(分set注入和构造器注入,其实本质就是xml里面的命令不同。这里注意,如果使用set那么定义类里面一定要有set函数,构造器注入,那么一定要有相应的构造器)

    普通属性:String, Interger (set和构造器:感觉还是set比较方便)

    <bean id="studentOne" class="com.atguigu.spring6.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>
    
    
    <bean id="studentTwo" class="com.atguigu.spring6.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
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    特殊属性:null,特殊的大于小于等(xml转义字符,cdata)

    <property name="name">
        <null />
    property>
    
    
    
    <property name="expression" value="a < b"/>
    
    <property name="expression">
        
        
        
        
        <value>value>
    property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    对象:外部bean,内部bean,级联(通过.来实现)

    外部bean
    <bean id="clazzOne" class="com.atguigu.spring6.bean.Clazz">
        <property name="clazzId" value="1111">property>
        <property name="clazzName" value="财源滚滚班">property>
    bean>
    <bean id="studentFour" class="com.atguigu.spring6.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>
    bean>
    内部bean
    <bean id="studentFour" class="com.atguigu.spring6.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">
            
            
            <bean id="clazzInner" class="com.atguigu.spring6.bean.Clazz">
                <property name="clazzId" value="2222">property>
                <property name="clazzName" value="远大前程班">property>
            bean>
        property>
    bean>
    级联(通过.来实现)
    
    <bean id="studentFour" class="com.atguigu.spring6.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
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    数组

        <property name="hobbies">
            <array>
                <value>抽烟value>
                <value>喝酒value>
                <value>烫头value>
            array>
        property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    List

    <bean id="clazzTwo" class="com.atguigu.spring6.bean.Clazz">
        <property name="clazzId" value="4444">property>
        <property name="clazzName" value="Javaee0222">property>
        <property name="students">
            <list>
                <ref bean="studentOne">ref>
                <ref bean="studentTwo">ref>
                <ref bean="studentThree">ref>
            list>
        property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Map

    <bean id="teacherOne" class="com.atguigu.spring6.bean.Teacher">
        <property name="teacherId" value="10010">property>
        <property name="teacherName" value="大宝">property>
    bean>
    
    <bean id="teacherTwo" class="com.atguigu.spring6.bean.Teacher">
        <property name="teacherId" value="10086">property>
        <property name="teacherName" value="二宝">property>
    bean>
    
    <bean id="studentFour" class="com.atguigu.spring6.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="hobbies">
            <array>
                <value>抽烟value>
                <value>喝酒value>
                <value>烫头value>
            array>
        property>
        <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>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    util(注意要修改头文件)

    
    <util:list id="students">
        <ref bean="studentOne">ref>
        <ref bean="studentTwo">ref>
        <ref bean="studentThree">ref>
    util:list>
    
    <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>
    <bean id="clazzTwo" class="com.atguigu.spring6.bean.Clazz">
        <property name="clazzId" value="4444">property>
        <property name="clazzName" value="Javaee0222">property>
        <property name="students" ref="students">property>
    bean>
    <bean id="studentFour" class="com.atguigu.spring6.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="hobbies">
            <array>
                <value>抽烟value>
                <value>喝酒value>
                <value>烫头value>
            array>
        property>
        <property name="teacherMap" ref="teacherMap">property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    使用p标签(注意要修改头文件)

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

    3.从外部对象中获取配置内容

    要修改对应的头文件。
    外部文件放在了resource目录下。

    
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.作用域

    在这里插入图片描述

    5.生命周期

    • bean对象创建(调用无参构造器)

    • 给bean对象设置属性

    • bean的后置处理器(初始化之前)

    • bean对象初始化(需在配置bean时指定初始化方法:自己设置)

    • bean的后置处理器(初始化之后)

    • bean对象就绪可以使用

    • bean对象销毁(需在配置bean时指定销毁方法:自己设置)

    • IOC容器关闭

    
    
    <bean class="com.atguigu.spring6.bean.User" scope="prototype" init-method="initMethod" destroy-method="destroyMethod">
        <property name="id" value="1001">property>
        <property name="username" value="admin">property>
        <property name="password" value="123456">property>
        <property name="age" value="23">property>
    bean>
    
    <bean id="myBeanProcessor" class="com.zy.spring6.iocxml.livestage.MyBeanProcesser"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这里的后置处理器需要实现BeanPostProcessor接口的两个方法(分别作用于初始化前和初始化后)。
    值得注意的是这个类需要在xml里面进行配置,且适用于容器管理所有的bean

    6.FactoryBean

    感觉就是在配置类里面套上了一个盒子。使得,在xml里面只要配置FactoryBean就好了。老师说是整合其他框架用的,暂时感受不到它的魅力。

    7.基于xml的自动装配

    老师设置的环境是JavaWeb常见的场景,controller-》service-》dao
    那么controller里面要创建一个service对象,service里面要创建一个dao对象。(值得注意,这里service和dao分别需要有接口和对应的实现类,与JavaWeb里面实现的一样。)
    IoC可以简化这个过程,只要在controller里创建一个service的属性并设置set方法,service里面也一样。只要在xml里面配置 好就会自动装配。
    在这里插入图片描述

  • 相关阅读:
    计算机毕业设计hadoop+spark+hive知识图谱酒店推荐系统 酒店数据分析可视化大屏 酒店爬虫 高德地图API 酒店预测系统 大数据毕业设计
    线程池的原理
    Lua学习笔记:探究package
    【考研】数据结构——线索二叉树
    南京邮电大学电工电子(数电)实验报告——计数器 & 移位寄存器
    基于FPGA:多目标运动检测(手把手教学①)
    通过plesk面板的文件管理器上传文件
    我的创作纪念日
    java计算机毕业设计校园失物招领管理系统源码+系统+mysql数据库+lw文档
    泰迪智能科技大模型数据智能实验室
  • 原文地址:https://blog.csdn.net/anncyuyan/article/details/136160721