首页 > 其他分享 >Spring-1-透彻理解Spring XML的Bean创建--IOC

Spring-1-透彻理解Spring XML的Bean创建--IOC

时间:2023-08-04 15:03:44浏览次数:33  
标签:XML 容器 ac -- Spring Bean StudentDao IOC public

学习目标

上一篇文章我们介绍了什么是Spring,以及Spring的一些核心概念,并且快速快发一个Spring项目,实现IOC和DI,今天具体来讲解IOC

能够说出IOC的基础配置和Bean作用域

了解Bean的生命周期

能够说出Bean的实例化方式

一、Bean的基础配置

问题导入

问题1:在<bean>标签上如何配置别名?

问题2:Bean的默认作用范围是什么?如何修改?

1 Bean基础配置【重点】

配置说明

2 Bean别名配置

配置说明

注意事项:

获取bean无论是通过id还是name获取,如果无法获取到,将抛出异常NoSuchBeanDefinitionException
NoSuchBeanDefinitionException: No bean named 'studentDaoImpl' available

代码演示

【第0步】创建项目名称为10_2_IOC_Bean的maven项目

【第一步】导入Spring坐标

  <dependencies>
      <!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.3.15</version>
      </dependency>

      <!-- 导入junit的测试包 -->
      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
          <version>5.8.2</version>
          <scope>test</scope>
      </dependency>

      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.28</version>
      </dependency>
  </dependencies>

【第二步】导入Student实体类

@Data
@ToString
@AllArgsConstructor
public class Student {
    private String name;
    private String address;
    private Integer age;

    private Integer status;
}

【第三步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类
package com.zbbmeta.dao;

public interface StudentDao {
    /**
     * 添加学生
     */
    void save();
}
package com.zbbmeta.dao.impl;

import com.zbbmeta.dao.StudentDao;

public class StudentDaoImpl implements StudentDao {
    @Override
    public void save() {
        System.out.println("DAO: 添加学生信息到数据库...");
    }
}

  • StudentService接口和StudentServiceImpl实现类
package com.zbbmeta.service;

public interface StudentService {
    /**
     * 添加学生
     */
    void save();
}

package com.zbbmeta.service.impl;

import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.dao.impl.StudentDaoImpl;
import com.zbbmeta.service.StudentService;

public class StudentServiceImpl implements StudentService {

    //创建成员对象
    private StudentDao studentDao = new StudentDaoImpl();
    @Override
    public void save() {

    }
}

【第四步】创建Spring配置文件在resources目录下,配置对应类作为Spring管理的bean对象

  • 定义application.xml配置文件并配置StudentDaoImpl
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    目标:熟悉bean标签详细的属性应用
    -->
    <!--
    name属性:可以设置多个别名,别名之间使用逗号,空格,分号等分隔
    -->
    <bean name="studentDao2,abc studentDao3" class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao"></bean>

</beans>

注意事项:bean定义时id属性和name中名称不能有重复的在同一个上下文中(IOC容器中)不能重复

【第四步】根据容器别名获取Bean对象

package com.zbbmeta;

import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class NameApplication {
    public static void main(String[] args) {
        /**
         * 从IOC容器里面根据别名获取对象执行
         */
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="abc"对象
        StudentDao studentDao = (StudentDao) ac.getBean("abc");
        //3.执行对象方法
        studentDao.save();
        //4.关闭容器
        ac.close();
    }
}

打印结果

3 Bean作用范围配置【重点】

配置说明

扩展: scope的取值不仅仅只有singleton和prototype,还有request、session、application、 websocket ,表示创建出的对象放置在web容器(tomcat)对应的位置。比如:request表示保存到request域中。

代码演示

在application.xml中配置prototype格式

  • 定义application.xml配置文件并配置StudentDaoImpl
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    目标:熟悉bean标签详细的属性应用
    -->
    <!--
    scope属性:定义bean的作用范围,一共有5个
         singleton: 设置单例创建对象(推荐,也是默认值),好处:节省资源
         prototype: 设置多例创建对象,每次从IOC容器获取的时候都会创建对象,获取多次创建多次。
         request: 在web开发环境中,IOC容器会将对象放到request请求域中,对象存活的范围在一次请求内。
                  请求开始创建对象,请求结束销毁对象
         session: 在web开发环境中,IOC容器会将对象放到session会话域中,对象存活的范围在一次会话内。
                  会话开始开始创建对象,会话销毁对象销毁。
         global-session: 是多台服务器共享同一个会话存储的数据。
    -->
    <bean class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao4" scope="prototype"></bean>

</beans>

根据容器别名获取Bean对象

package com.zbbmeta;

import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ScopeApplication {
    public static void main(String[] args) {
        /**
         * Bean的作用域范围演示
         */
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="studentService"对象
        System.out.println("=========singleton(单例)模式=========");
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
        StudentDao studentDao1 = (StudentDao) ac.getBean("studentDao");
        System.out.println("studentDao = " + studentDao);
        System.out.println("studentDao1 = " + studentDao1);
        System.out.println("=========prototype模式=========");
        StudentDao studentDao2 = (StudentDao) ac.getBean("studentDao4");
        StudentDao studentDao3 = (StudentDao) ac.getBean("studentDao4");
        System.out.println("studentDao2 = " + studentDao2);
        System.out.println("studentDao3 = " + studentDao3);
        //4.关闭容器
        ac.close();
    }
}

打印结果

注意:在我们的实际开发当中,绝大部分的Bean是单例的,也就是说绝大部分Bean不需要配置scope属性

二、Bean的实例化

思考:Bean的实例化方式有几种?

2 实例化Bean的三种方式

2.1 构造方法方式【重点】

  • BookDaoImpl实现类
public class StudentDaoImpl implements StudentDao {
    public StudentDaoImpl() {
        System.out.println("Student dao constructor is running ....");
    }

    @Override
    public void save() {
        System.out.println("DAO: 添加学生信息到数据库...");
    }
}
  • application.xml配置
    <!--
    目标:讲解bean创建方式
    -->
    <!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建-->
    <bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>
  • AppForInstanceBook测试类
public class OneApplication {
    public static void main(String[] args) {
        /**
         * 无参构造方式创建Bean
         */
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="studentService"对象
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
        //3.执行对象方法
        studentDao.save();
        //4.关闭容器
        ac.close();
    }
}
  • 运行结果

注意:无参构造方法如果不存在,将抛出异常BeanCreationException

2.2 静态工厂方式

  • StudentDaoFactory工厂类
package com.zbbmeta.factory;

import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.dao.impl.StudentDaoImpl;

public class StudentDaoFactory {
//    静态工厂创建对象
    public static StudentDao getStudentDao(){
        System.out.println("Student static factory setup....");
        return new StudentDaoImpl();
    }
}
  • applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
    目标:讲解bean创建方式
    -->
    <!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建-->
<!--    <bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>-->

    <!--创建StudentDaoImpl对象方式2:调用静态工厂方法创建对象加入IOC容器
    class="com.zbbmeta.factory.StudentDaoFactory" 设置工厂类全名
    factory-method="getStudentDao" 调用工厂的静态方法
-->
    <bean class="com.zbbmeta.factory.StudentDaoFactory" factory-method="getStudentDao" id="studentDao2"></bean>


</beans>

注意:测试前最好把之前使用Bean标签创建的对象进行注释

  • TwoApplication测试类
public class TwoApplication {
    public static void main(String[] args) {
        /**
         * 无参构造方式创建Bean
         */
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="studentService"对象
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao2");
        //3.执行对象方法
        studentDao.save();
        //4.关闭容器
        ac.close();
    }
}
  • 运行结果

2.3 实例工厂方式

  • UserDao接口和UserDaoImpl实现类

    //利用实例方法创建StudentDao对象
    public StudentDao getStudentDao2(){
        System.out.println("调用了实例工厂方法");
        return new StudentDaoImpl();
    }
  • StudentDaoFactory工厂类添加方法
//实例工厂创建对象
public class UserDaoFactory {
    public UserDao getUserDao(){
        return new UserDaoImpl();
    }
}
  • applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--
    目标:讲解bean创建方式
    -->
    <!--创建StudentDaoImpl对象方式1:默认调用类的无参构造函数创建-->
<!--    <bean id="studentDao" class="com.zbbmeta.dao.impl.StudentDaoImpl"/>-->

    <!--创建StudentDaoImpl对象方式2:调用静态工厂方法创建对象加入IOC容器
    class="com.zbbmeta.factory.StudentDaoFactory" 设置工厂类全名
    factory-method="getStudentDao" 调用工厂的静态方法
-->
<!--    <bean class="com.zbbmeta.factory.StudentDaoFactory" factory-method="getStudentDao" id="studentDao2"></bean>-->


    <!--创建BookDaoImpl对象方式3:调用实例工厂方法创建对象加入IOC容器
    class="com.itheima.factory.BookDaoFactory" 设置工厂类全名
    factory-method="getBookDao" 调用工厂的静态方法
-->
    <!--第一步:创建工厂StudentDaoFactory对象-->
    <bean class="com.zbbmeta.factory.StudentDaoFactory" id="studentDaoFactory"></bean>
    <!--第一步:调用工厂对象的getStudentDao2()实例方法创建StudentDaoImpl对象加入IOC容器
        factory-bean="studentDaoFactory" 获取IOC容器中指定id值的对象
        factory-method="getStudentDao2" 如果配置了factory-bean,那么这里设置的就是实例方法名
    -->
    <bean factory-bean="studentDaoFactory" factory-method="getStudentDao2" id="studentDao3"></bean>


</beans>
  • ThreeApplication测试类
package com.zbbmeta;

import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ThreeApplication {
    public static void main(String[] args) {
        /**
         * 无参构造方式创建Bean
         */
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="studentService"对象
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao3");
        //3.执行对象方法
        studentDao.save();
        //4.关闭容器
        ac.close();
    }
}

  • 运行结果

三、Bean的生命周期【了解】

问题导入

问题1:多例的Bean能够配置并执行销毁的方法?

问题2:如何做才执行Bean销毁的方法?

1 生命周期相关概念介绍

  • 生命周期:从创建到消亡的完整过程
  • bean生命周期:bean从创建到销毁的整体过程
  • bean生命周期控制:在bean创建后到销毁前做一些事情

1.1生命周期过程

  • 初始化容器
    • 创建对象(内存分配)
    • 执行构造方法
    • 执行属性注入(set操作)
    • 执行bean初始化方法
  • 使用bean
    • 执行业务操作
  • 关闭/销毁容器
    • 执行bean销毁方法

2 代码演示

2.1 Bean生命周期控制

【第0步】创建项目名称为10_4_IOC_BeanLifeCycle的maven项目

【第一步】导入Spring坐标

  <dependencies>
      <!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.3.15</version>
      </dependency>

      <!-- 导入junit的测试包 -->
      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
          <version>5.8.2</version>
          <scope>test</scope>
      </dependency>

      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.28</version>
      </dependency>
  </dependencies>

【第二步】导入Student实体类

@Data
@ToString
@AllArgsConstructor
public class Student {
    private String name;
    private String address;
    private Integer age;

    private Integer status;
}

【第三步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类
package com.zbbmeta.dao;

public interface StudentDao {
    /**
     * 添加学生
     */
    void save();
}
package com.zbbmeta.dao.impl;

import com.zbbmeta.dao.StudentDao;

public class StudentDaoImpl implements StudentDao {

    public StudentDaoImpl(){
        System.out.println("Student Dao 的无参构造");
    }

    @Override
    public void save() {
        System.out.println("DAO: 添加学生信息到数据库...");
    }

    public void init(){
        System.out.println("Student Dao 的初始化方法");
    }

    public void destroy(){
        System.out.println("Student Dao 的销毁方法");
    }
}

【第四步】创建Spring配置文件在resources目录下,配置对应类作为Spring管理的bean对象

  • 定义application.xml配置文件并配置StudentDaoImpl
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--目标:创建StudentDaoImpl对象:设置生命周期方法
        init-method="init" 在对象创建后立即调用初始化方法
        destroy-method="destroy":在容器执行销毁前立即调用销毁的方法
        注意:只有单例对象才会运行销毁生命周期方法
-->
    <bean class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao" init-method="init" destroy-method="destroy"></bean>
</beans>

【第四步】根据容器别名获取Bean对象

package com.zbbmeta;

import com.zbbmeta.dao.StudentDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LifeCycleApplication {
    public static void main(String[] args) {


            //1.根据配置文件application.xml创建IOC容器
            ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
            //2.从IOC容器里面获取id="studentService"对象
            StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
            //3.执行对象方法
            studentDao.save();
            //4.关闭容器
            ac.close();
        
    }
}

打印结果

3 Bean销毁时机

  • 容器关闭前触发bean的销毁
  • 关闭容器方式:
    • 手工关闭容器 调用容器的close()操作
    • 注册关闭钩子(类似于注册一个事件),在虚拟机退出前先关闭容器再退出虚拟机 调用容器的registerShutdownHook()操作
public class LifeCycleApplication {
    public static void main(String[] args) {


            //1.根据配置文件application.xml创建IOC容器
            ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
            //2.从IOC容器里面获取id="studentService"对象
            StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
            //3.执行对象方法
            studentDao.save();
            //4.关闭容器
//            ac.close();
        //注册关闭钩子函数,在虚拟机退出之前回调此函数,关闭容器
        ac.registerShutdownHook();
    }
}

标签:XML,容器,ac,--,Spring,Bean,StudentDao,IOC,public
From: https://blog.51cto.com/u_12023894/6962630

相关文章

  • 国标GB28181视频平台LntonGBS(源码版)国标平台正确调阅实时录像接口的具体操作步骤
    LntonGBS之所以成为安防市场的主流视频平台,主要得益于其架构优势。首先,LntonGBS采用了云边端一体化的架构,将云计算、边缘计算和终端设备有机结合,实现了数据的高效传输和处理。这种架构不仅能够满足大规模视频数据的存储和分析需求,还能够实现实时监控和快速响应,提高了安防系统的整体......
  • 【数据结构 | 入门】线性表与链表 (问题引入&实现&算法优化)
    ......
  • 零成本实现Zabbix语音告警
    一、场景模拟国内某知名大型证券企业A公司,在使用开源Zabbix监控平台时发现Zabbix只能依据告警级别产生相应的声音通知,却无法对告警信息进行语音播报。为此,运维同事小东自告奋勇为公司打造了一套“零成本的最简单的Zabbix语音告警”方案,实现与Zabbix的告警对接,为公司省下10W+的运维......
  • nio/mina/openfire阅读笔记
    minajavadocMina-模拟同步请求Mina源码分析http://michael-softtech.iteye.com/blog/1145458Mina2.0框架源码剖析(一)Mina2.0框架源码剖析(二)Mina2.0框架源码剖析(三)Mina2.0框架源码剖析(四)Mina2.0框架源码剖析(五)Mina2.0框架源码剖析(六)Mina2.0框架源码剖析(七)Mina2.0框架......
  • 视频融合平台视频汇聚平台LiteCVR接入协议通道播放失败反馈处理案例
    我们近期正在整理以前到近期的用户反馈,今后将陆续把已经整理好的案列以图文的形式分享给大家参考。有用户反馈称,在他们现场部署了LiteCVR平台后,NVR设备通过国标GB28181协议成功接入到LiteCVR。尽管设备和通道都显示为在线状态,但是他们发现部分通道的视频无法正常播放。因此,他们请求......
  • DELL服务器配置RAID图文教程
    dell服务器创建Raid5,(适用于DellR730、R720、R630、R620、R420、R530)DiskGroup:磁盘组,这里相当于是阵列,例如配置了一个RAID5,就是一个磁盘组。VD(VirtualDisk):虚拟磁盘,虚拟磁盘可以不使用阵列的全部容量,也就是说一个磁盘组可以分为多个VD。PD(PhysicalDisk):物理磁盘。Mgmt:管理。1、重......
  • 个人微信开发API接口
    个人微信开发API接口可拓展功能说明1、个人微信多账号管理、聚合聊天、多个微信号同时登陆、多个微信号集中在一个窗口进行聊天,实现一人多号同时沟通快速提升沟通效率,提升微信营销效率。2、客服灵活分配:客服主管可自由分配微信号给指定客服,方便及时处理问题,也可随时转接给公司商务......
  • 往年 | 大疆雷达算法校招笔试题目解析
    2023年度会员内容更新公告(04.16)序号类别内容文件路径1无无无【正文】编辑|小助理 审核|调皮哥本文图片中的答案仅仅是为了说明题目,并不是正确答案,额外注意!文中的解析仅作为学习参考。1、FFT主要利用旋转因子  的什么性质,减少运算量?答:对称性和周期性2、采样率fs,采样M个实数,进行N......
  • 学雷达怎么入门?雷达专业必备的理论书籍
    1.引言大家好!我是调皮哥,目前研三,尚未毕业,研究生阶段主要是做一些毫米波雷达的应用,对线性调频连续波雷达的一些基本原理算是有一些了解吧。如今盲审已过,剩下的时间准备把手头的项目做做,接着准备毕业论文答辩,然后在有空的时候顺便给大家分享一些雷达方面的学习经验、问题解答,以及做一......
  • 国标GB28181视频平台LntonGBS(源码版)国标视频平台优化设备通道视频播放出现跳屏的问题
    LntonGBS国标视频云服务支持设备/平台通过国标GB28181协议注册接入,并能实现视频的实时监控直播、录像、检索与回看、语音对讲、云存储、告警、平台级联等功能。平台部署简单、可拓展性强,支持将接入的视频流进行全终端、全平台分发,分发的视频流包括RTSP、RTMP、FLV、HLS、WebRTC等格......