优质全套Spring全套教程一

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 优质全套Spring全套教程一

hello,我是小索奇,这里把Spring全套笔记分享出来哈,便于大家查看~一起加油

Spring

1、Spring简介

1.1、Spring概述


官网地址:Spring | Home


Spring 是最受欢迎的企业级 Java 应用程序开发框架,数以百万的来自世界各地的开发人员使用


Spring 框架来创建性能好、易于测试、可重用的代码。


Spring 框架是一个开源的 Java 平台,它最初是由 Rod Johnson 编写的,并且于 2003 年 6 月首


次在 Apache 2.0 许可下发布。


Spring 是轻量级的框架,其基础版本只有 2 MB 左右的大小。


Spring 框架的核心特性是可以用于开发任何 Java 应用程序,但是在 Java EE 平台上构建 web 应


用程序是需要扩展的。 Spring 框架的目标是使 J2EE 开发变得更容易使用,通过启用基于 POJO


编程模型来促进良好的编程实践。


框架主要就是为了增加扩展性

1.2、Spring家族

Spring是一个庞大的家族,其中有实现各种功能的框架

项目列表:Spring | Projects


1.3、Spring Framework

Spring 基础框架,可以视为 Spring 基础设施,基本上任何其他 Spring 项目、框架都是以 Spring Framework为基础的。

1.3.1、Spring Framework特性


非侵入式:使用 Spring Framework 开发应用程序时,Spring 对应用程序本身的结构影响非常


小。对领域模型可以做到零污染;对功能性组件也只需要使用几个简单的注解进行标记,完全不会


破坏原有结构,反而能将组件结构进一步简化。这就使得基于 Spring Framework 开发应用程序


时结构清晰、简洁优雅。


控制反转:IOC——Inversion of Control,翻转资源获取方向。把自己创建资源、向环境索取资源


变成环境将资源准备好,我们享受资源注入。把对象的控制权翻转给程序本身(教给spring来管理我们的对象)


本来是主动创建对象,现在是被动接收对象,框架为我们提供对象-配置在xml中,降低耦合性,降低对象之间的依赖关系


面向切面编程(横向抽取):AOP——Aspect Oriented Programming,在不修改源代码的基础上增强代码功


能。


容器:Spring IOC 是一个容器,因为它包含并且管理组件对象的生命周期(帮我们管理对象)。组件享受到了容器化


的管理,替程序员屏蔽了组件创建过程中的大量细节,极大的降低了使用门槛,大幅度提高了开发效率。


对我们创建对象过程进行了屏蔽,我们只需要关心如何使用即可,不需要关心如何创建的对象


组件化:Spring 实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用 XML


我们给ioc所管理的对象,它在ioc中就叫做组件


和 Java 注解组合这些对象。这使得我们可以基于一个个功能明确、边界清晰的组件有条不紊的搭


建超大型复杂应用系统。


声明式:很多以前需要编写代码才能实现的功能,现在只需要声明需求即可由框架代为实现。


编程式具体功能什么的由我们自己去实现


一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(可利用我们spring的两大核心去整合我们的第三方框架MyBatis)。而且


Spring 旗下的项目已经覆盖了广泛领域,很多方面的功能性需求可以在 Spring Framework 的基础上全部使用 Spring 来实现。

1.3.2、Spring Framework五大功能模块
功能模块 功能介绍
Core Container IOC核心容器,在 Spring 环境下使用任何功能都必须基于 IOC 容器。
AOP&Aspects 第二个核心:面向切面编程
Testing 提供了对 junit 或 TestNG 测试框架的整合。
Data Access/Integration 提供了对数据访问/集成的功能。
Spring MVC 提供了面向Web应用程序的集成功能。

2、IOC

2.1、IOC容器

2.1.1、IOC思想

IOC:Inversion of Control,翻译过来是反转控制


①获取资源的传统方式

自己做饭:买菜、洗菜、择菜、改刀、炒菜,全过程参与,费时费力,必须清楚了解资源创建整个过程中的全部细节且熟练掌握。


在应用程序中的组件需要获取资源时,传统的方式是组件主动的从容器中获取所需要的资源,在这样的

模式下开发人员往往需要知道在具体容器中特定资源的获取方式,增加了学习成本,同时降低了开发效率。

②反转控制方式获取资源


点外卖:下单、等、吃,省时省力,不必关心资源创建过程的所有细节。


反转控制的思想完全颠覆了应用程序组件获取资源的传统方式:反转了资源的获取方向——改由容器主动的将资源推送给需要的组件,开发人员不需要知道容器是如何创建资源对象的,只需要提供接收资源的方式即可,极大的降低了学习成本,提高了开发的效率。这种行为也称为查找的被动形式。

③DI

DI:Dependency Injection,翻译过来是依赖注入。就是为我们当前所管理的依赖赋值


DI 是 IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如:setter 方法)接受来自于容器


的资源注入。相对于IOC而言,这种表述更直接。


所以结论是:IOC 就是一种反转控制的思想, 而 DI 是对 IOC 的一种具体实现。

2.1.2、IOC容器在Spring中的实现

  • 实际上就是spring一系列的api
  • 我们把对象教给IOC管理之后,我们要想获取IOC容器所管理的对象,我们要先获取IOC容器


Spring 的 IOC 容器就是 IOC 思想的一个落地的产品实现。IOC 容器中管理的组件也叫做 bean。在创建bean 之前,首先需要创建 IOC 容器。Spring 提供了 IOC 容器的两种实现方式:

①BeanFactory
  • 既然叫Factory,那么用的就是Factory模式


这是 IOC 容器的基本实现,是 Spring 内部使用的接口。面向 Spring 本身,不提供给开发人员使用。我们程序媛用ApplicationContext

②ApplicationContext

BeanFactory 的子接口,提供了更多高级特性(子接口肯定要比父接口的功能多)。面向 Spring 的使用者,几乎所有场合都使用

ApplicationContext 而不是底层的 BeanFactory。

  • 接口:需要向上转型,创建IOC对象
③ApplicationContext的主要实现类

类型名 简介
ClassPathXmlApplicationContext 通过读取类路径下的 XML 格式的配置文件创建 IOC 容器对象
FileSystemXmlApplicationContext 通过文件系统路径读取 XML 格式的配置文件创建 IOC 容器对象
ConfigurableApplicationContext ApplicationContext 的子接口,包含一些扩展方法refresh() 和 close() ,让 ApplicationContext 具有启动、关闭和刷新上下文的能力。
WebApplicationContext 专门为 Web 应用准备,基于 Web 环境创建 IOC 容器对象,并将对象引入存入 ServletContext 域中。
  • 配置文件一般都写到类目录下(java下的resources中),resources会被加载到类目录下,ClassPathXmlApplicationContext用的多
  • FileSystemXml是文件系统,从磁盘上某一个地方的xml,通过这个xml读取IOC容器


2.2、基于XML管理bean

  • spring来管理对象,管理的对象叫做主键,也叫做bean
  • 管理方式共有两种:
  • 基于xml
  • 基于注解方式


2.2.1、实验一:入门案例

①创建Maven Module
②引入依赖

spring-context:spring上下文,依赖具有传递性,spring-conntext所依赖的jar包也会被传递进来


<dependencies>
     <!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>5.3.1</version>
     </dependency>
     <!-- junit测试 -->
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
         <scope>test</scope>
     </dependency>
 </dependencies>


③创建类HelloWorld
public class HelloWorld {
     public void sayHello(){
         System.out.println("helloworld");
     }
 }

  • 现在可以指定某个xml来获取IOC容器,我们获取ioc容器时候是可以指定的,这里xml名字可以是不固定的
  • 整合SSM时候就是固定了
⑤在Spring的配置文件中配置bean
  • spring默认提供的对象是单例的,所以我们通过获取这个bean就可以来使用了
 <!--
     配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理
     通过bean标签配置IOC容器所管理的bean
     属性:
         id:设置bean的唯一标识
         class:设置bean所对应类型的全类名
 -->
 <bean id="helloworld" class="com.atguigu.spring.bean.HelloWorld"></bean>
⑥创建测试类测试
  • ioc.getBean(name)这个name就是bean的唯一标识:id,
  • 通过IOC获取的,我们不知道类型,我们需要强转


 @Test
 public void testHelloWorld(){
     ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
     HelloWorld helloworld = (HelloWorld) ioc.getBean("helloworld");
     helloworld.sayHello();
 }
⑦思路

在IOC容器中它就是通过反射+工厂模式来帮助我们创建对象,管理对象的

默认使用的就是无参构造,不知道我们的有参构造是什么,当把无参构造删除时候,执行会发现出现错误(NoSuchMethodException)


以后设置类的时候别忘记写无参构造,我们没写有参构造,无参构造是默认的

⑧注意


Spring 底层默认通过反射技术调用组件类的无参构造器来创建组件对象,这一点需要注意。如果在需要无参构造器时,没有无参构造器,则会抛出下面的异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name

'helloworld' defined in class path resource [applicationContext.xml]: Instantiation of bean

failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed

to instantiate [com.atguigu.spring.bean.HelloWorld]: No default constructor found; nested

exception is java.lang.NoSuchMethodException: com.atguigu.spring.bean.HelloWorld.

<init>()

2.2.2、实验二:获取bean

①方式一:根据id获取

由于 id 属性指定了 bean 的唯一标识,所以根据 bean 标签的 id 属性可以精确获取到一个组件对象。

上个实验中我们使用的就是这种方式。

②方式二:根据类型获取

ac也就是ioc

@Test
 public void testHelloWorld(){
     ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
     HelloWorld bean = ac.getBean(HelloWorld.class);
     bean.sayHello();
 }
③方式三:根据id和类型
  • 进一步筛选,可以放在IOC容器有两个相同的class路径
@Test
 public void testHelloWorld(){
     ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
     HelloWorld bean = ac.getBean("helloworldOne", HelloWorld.class);
     bean.sayHello();
 }
④注意

当根据类型获取bean时,要求IOC容器中指定类型的bean有且只能有一个,否则报错,但是这种情况很少出现,我们用的做多的还是方式二根据类型获取


class里面只能写的是一个具体类型,接口啥的是没有构造方法的,不能newInstance(但是扩展到这个类上的接口是可以用的)

当IOC容器中一共配置了两个:

<bean id="helloworldOne" class="com.atguigu.spring.bean.HelloWorld"></bean>
 <bean id="helloworldTwo" class="com.atguigu.spring.bean.HelloWorld"></bean>

根据类型获取时会抛出异常:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean

of type 'com.atguigu.spring.bean.HelloWorld' available: expected single matching bean but

found 2: helloworldOne,helloworldTwo
⑤扩展

如果组件类实现了接口,根据接口类型可以获取 bean 吗?

可以,前提是bean唯一

只要是这个对象实现的接口,和这个对象有关就可以获取到这个bean(管理的对象)

  • Class Student implements Person

接口拓展:


boolean result = obj instanceof Class;


其中,obj指的是对象,class为类或者接口,该表达式的含义为:当obj是class的对象,或者是其直接/间接子类,或者是其接口的实现类时,result为true,否则为false。


如果一个接口有多个实现类,这些实现类都配置了 bean,根据接口类型可以获取 bean 吗?

不行,因为bean不唯一

⑥结论

根据类型来获取bean时,在满足bean唯一性的前提下,其实只是看:『对象 instanceof 指定的类

型』的返回结果,只要返回的是true就可以认定为和类型匹配,能够获取到(即通过bean继承的,实现的类型都可以获取到bean)。

2.2.3、实验三:依赖注入(DI)之setter注入

IOC是从我们当前获取资源的角度来说,我们以前需要主动获取,现在被动接受,


比如我们Student里面有id、name、age、sex,那么我们就是Student是依赖于id、name、age、sex属性,既然这个对象依赖于这些属性,那么我们就可以在IOC中为它所依赖的属性赋值


依赖注入:就是对我们当前类中的属性赋值的过程就叫做依赖注入


把方法名中的get、set去掉,剩余部分的首字母变为小写就是属性,严格来说属性指的就是我们的get和set方法

①创建学生类Student
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
    public Student() {
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", age=" + age +
            ", sex='" + sex + '\'' +
            '}';
    }
}
②配置bean时为属性赋值
  • DI依赖注入第一种方式set注入(它底层用的是set注入)
<bean id="studentTwo" class="com.atguigu.spring.pojo.Student">
    <!-- property标签:通过组件类的setXxx()方法给组件对象设置属性 -->
    <!-- name属性:指定属性名(这个属性名是getXxx()、setXxx()方法定义的,和成员变量无关)-->
    <!-- value属性:指定属性值 -->
    <property name="id" value="1001"></property>
    <property name="name" value="张三"></property>
    <property name="age" value="23"></property>
    <property name="sex" value="男"></property>
</bean>
③测试
public void testDIBySet(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("spring-ioc.xml");
    Student studentTwo = ac.getBean("studentTwo", Student.class);//这里测试用id和类型,用的多的还是根据类型获取
    System.out.println(studentTwo);
}

2.2.4、实验四:依赖注入之构造器注入

①在Student类中添加有参构造
public Student(Integer id, String name, Integer age, String sex) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.sex = sex;
}
②配置bean
<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>

注意:

constructor-arg标签还有两个属性可以进一步描述构造器参数:

  • index属性:指定参数所在位置的索引(从0开始)
  • name属性:指定参数名
③测试
@Test
public void testDIBySet(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("springdi.xml");
    Student studentOne = ac.getBean("studentTwo", Student.class);
    System.out.println(studentOne);
}

问题:如果有两个相同的有参构造匹配哪一个?

这里默认匹配的是第二个score,如果要指定类型一定设置name给想要给的属性


2.2.5、实验五:特殊值处理

①字面量赋值

什么是字面量?


int a = 10;


声明一个变量a,初始化为10,此时a就不代表字母a了,而是作为一个变量的名字。当我们引用a


的时候,我们实际上拿到的值是10。


而如果a是带引号的:'a',那么它现在不是一个变量,它就是代表a这个字母本身,这就是字面


量。所以字面量没有引申含义,就是我们看到的这个数据本身。


基本数据类型及其包装类,和String都是字面量


value是专门给字面量赋值的,直接在里面写个null?不可能的,她在这里表示字符串null



②null值
<property name="name">
  <null />
</property>

注意:

<property name="name" value="null"></property>

以上写法,为name所赋的值是字符串null

如何知道控制台输出的是空字符串还是null?

  • 我们在输出时候.toString即可,不报错就是字符串


如何给属性设置null?

  • 在里面设置null标签即可(单双标签都可:),也可以在里面设置子标签value,不写在property里面也可以


③xml实体
  • xml是可扩展标识语言
  • 它和html相似之处就是不能用特殊字符,比如value="<王五>" 这个标签就不对,这个尖括号就是特殊字符,如何解决这个问题?

用这个特殊字符所对应的实体,<是< ,>是>

可以百度搜索 实体

<!-- 小于号在XML文档中用来定义标签的开始,不能随便使用 -->
<!-- 解决方案一:使用XML实体来代替 -->
<property name="expression" value="a &lt; b"/>
④CDATA节

放在CDATA节里面的内容都会被原样解析,不用实体也可

  • 它是xml中的一个特殊的标签,不能直接写到property中
  • 本身就是一个特殊的标签,只能以标签方式写,所以是不能在属性里面直接写的,可以在子标签里面写
  • 在idea中直接输入大写CD就可以快速生成
<property name="expression">
    <!-- 解决方案二:使用CDATA节 -->
    <!-- CDATA中的C代表Character,是文本、字符的含义,CDATA就表示纯文本数据 -->
    <!-- XML解析器看到CDATA节就知道这里是纯文本,就不会当作XML标签或属性来解析 -->
    <!-- 所以CDATA节中写什么符号都随意 -->
    <value><![CDATA[a < b]]></value>
</property>

2.2.6、实验六:为类类型属性赋值

①创建班级类Clazz(因为Class是关键字,所以Clazz)
public class Clazz {
    private Integer clazzId;
    private String clazzName;
    public Integer getClazzId() {
        return clazzId;
    }
    public void setClazzId(Integer clazzId) {
        this.clazzId = clazzId;
    }
    public String getClazzName() {
        return clazzName;
    }
    public void setClazzName(String clazzName) {
        this.clazzName = clazzName;
    }
    @Override
    public String toString() {
        return "Clazz{" +
            "clazzId=" + clazzId +
            ", clazzName='" + clazzName + '\'' +
            '}';
    }
    public Clazz() {
    }
    public Clazz(Integer clazzId, String clazzName) {
        this.clazzId = clazzId;
        this.clazzName = clazzName;
    }
}
②修改Student类

在Student类中添加以下代码:

private Clazz clazz;
public Clazz getClazz() {
    return clazz;
}
public void setClazz(Clazz clazz) {
    this.clazz = clazz;
}
③方式一:引用外部已声明的bean
  • 对1对对象,对多对集合
  • 这里我把配置Clazz类型的bean:"com.atguigu.spring.bean.Clazz"改为了"com.atguigu.spring.pojo.Clazz" ,和视频老师的一样

<bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
    <property name="clazz.cid" value="1111"></property>
    <property name="clazz.cname" value="财源滚滚班"></property>
</bean>

为Student中的clazz属性赋值:

<bean id="studentFour" class="com.atguigu.spring.pojo.Student">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value="女"></property>
    <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
    <property name="clazz" ref="clazzOne"></property>
</bean>

错误演示:

value是给字面量赋值的

<bean id="studentFour" class="com.atguigu.spring.pojo.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" value="clazzOne"></property>
</bean>

如果错把ref属性写成了value属性,会抛出异常: Caused by: java.lang.IllegalStateException:


Cannot convert value of type 'java.lang.String' to required type


'com.atguigu.spring.bean.Clazz' for property 'clazz': no matching editors or conversion


strategy found


意思是不能把String类型转换成我们要的Clazz类型,说明我们使用value属性时,Spring只把这个


属性看做一个普通的字符串,不会认为这是一个bean的id,更不会根据它去找到bean来赋值

④方式二:内部bean
<bean id="studentFour" class="com.atguigu.spring.pojo.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中再声明一个bean就是内部bean -->
        <!-- 内部bean只能用于给属性赋值,不能在外部通过创建IOC容器获取,因此可以省略id属性 -->
        <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
            <property name="cid" value="2222"></property>
            <property name="cname" value="远大前程班"></property>
        </bean>
    </property>
</bean>
③方式三:级联属性赋值
  • 一般不用这种,麻烦

和MyBatis不一样的地方:一定先引用某个bean为属性赋值或者实例化,才可以使用级联方式更新属性,使用点.

<bean id="studentFour" class="com.atguigu.spring.pojo.Student">
    <property name="id" value="1004"></property>
    <property name="name" value="赵六"></property>
    <property name="age" value="26"></property>
    <property name="sex" value="女"></property>
    <!-- 一定先引用某个bean为属性赋值或者实例化,才可以使用级联方式更新属性 -->
    <property name="clazz" ref="clazzOne"></property>
    <property name="clazz.cid" value="3333"></property>
    <property name="clazz.cname" value="最强王者班"></property>
</bean>

2.2.7、实验七:为数组类型属性赋值

①修改Student类

在Student类中添加以下代码:

private String[] hobbies;
public String[] getHobbies() {
    return hobbies;
}
public void setHobbies(String[] hobbies) {
    this.hobbies = hobbies;
}
②配置bean
  • 里面使用的标签取决于类类型还是字面量(前者用ref表示引用,后者用value)


<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>
    <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
    <property name="clazz" ref="clazzOne"></property>
    <property name="hobbies">
        <array>
            <value>抽烟</value>
            <value>喝酒</value>
            <value>烫头</value>
        </array>
    </property>
</bean>

2.2.8、实验八:为集合类型属性赋值

①为List集合类型属性赋值

在Clazz类中添加以下代码:内部bean

private List<Student> students;
public List<Student> getStudents() {
    return students;
}
public void setStudents(List<Student> students) {
    this.students = students;
}
配置bean:

<bean id="clazzTwo" class="com.atguigu.spring.bean.Clazz">
    <property name="cid" value="4444"></property>
    <property name="cname" value="Javaee0222"></property>
    <property name="students">
        <list>
            <ref bean="studentOne"></ref>
            <ref bean="studentTwo"></ref>
            <ref bean="studentThree"></ref>
        </list>
    </property>
</bean>


配置bean:

<bean id="clazzTwo" class="com.atguigu.spring.bean.Clazz">
    <property name="cid" value="4444"></property>
    <property name="cname" value="Javaee0222"></property>
    <property name="students">
        <list>
            <ref bean="studentOne"></ref>
            <ref bean="studentTwo"></ref>
            <ref bean="studentThree"></ref>
        </list>
    </property>
</bean>

若为Set集合类型属性赋值,只需要将其中的list标签改为set标签即可

集合方式引入约束:

  • utils:list是设置集合类型的bean
  • utils:map设置map集合类型的bean


一开始有beans约束才能用beans,现在加上

引用studentList



②为Map集合类型属性赋值

创建教师类Teacher:

public class Teacher {
    private Integer teacherId;
    private String teacherName;
    public Integer getTeacherId() {
        return teacherId;
    }
    public void setTeacherId(Integer teacherId) {
        this.teacherId = teacherId;
    }
    public String getTeacherName() {
        return teacherName;
    }
    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }
    public Teacher(Integer teacherId, String teacherName) {
        this.teacherId = teacherId;
        this.teacherName = teacherName;
    }
    public Teacher() {
    }
    @Override
    public String toString() {
        return "Teacher{" +
            "teacherId=" + teacherId +
            ", teacherName='" + teacherName + '\'' +
            '}';
    }
}

在Student类中添加以下代码:


private Map<String, Teacher> teacherMap;
public Map<String, Teacher> getTeacherMap() {
    return teacherMap;
}
public void setTeacherMap(Map<String, Teacher> teacherMap) {
    this.teacherMap = teacherMap;
}

配置bean:

  • entry表示键和值
  • value-ref引用某一个bean作为value值
  • 如果键是字面量就用key,类类型用key-ref,value同理
spring-ioc.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" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="studentOne" class="com.atguigu.spring.pojo.Student"></bean>

    <bean id="studentTwo" class="com.atguigu.spring.pojo.Student">
        <!--
            property:通过成员变量的set方法进行赋值
            name:设置需要赋值的属性名(和set方法有关)
            value:设置为属性所赋的值
        -->
        <property name="sid" value="1001"></property>
        <property name="sname" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="gender" value="男"></property>
    </bean>

    <bean id="studentThree" class="com.atguigu.spring.pojo.Student">
        <constructor-arg value="1002"></constructor-arg>
        <constructor-arg value="李四"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
        <constructor-arg value="24" name="age"></constructor-arg>
    </bean>

    <bean id="studentFour" class="com.atguigu.spring.pojo.Student">
        <property name="sid" value="1003"></property>
        <!--
            <:&lt;
            >:&gt;
            CDATA节其中的内容会原样解析<![CDATA[...]]>
            CDATA节是xml中一个特殊的标签,因此不能写在一个属性中
        -->
        <!--<property name="sname" value="&lt;王五&gt;"></property>-->
        <property name="sname">
            <value><![CDATA[<王五>]]></value>
        </property>
        <property name="gender">
            <null />
        </property>
    </bean>

    <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
        <property name="sid" value="1004"></property>
        <property name="sname" value="赵六"></property>
        <property name="age" value="26"></property>
        <property name="gender" value="男"></property>
        <!--ref:引用IOC容器中的某个bean的id-->
        <!--<property name="clazz" ref="clazzOne"></property>-->
        <!--级联的方式,要保证提前为clazz属性赋值或者实例化-->
        <!--<property name="clazz.cid" value="2222"></property>
        <property name="clazz.cname" value="远大前程班"></property>-->
        <property name="clazz">
            <!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
            <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
                <property name="cid" value="2222"></property>
                <property name="cname" value="远大前程班"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
                <value>抽烟</value>
                <value>喝酒</value>
                <value>烫头</value>
            </array>
        </property>
        <property name="teacherMap" ref="teacherMap"></property>
        <!--<property name="teacherMap">
            <map>
                <entry key="10086" value-ref="teacherOne"></entry>
                <entry key="10010" value-ref="teacherTwo"></entry>
            </map>
        </property>-->
    </bean>

    <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="最强王者班"></property>
        <property name="students" ref="studentList"></property>
        <!--<property name="students">
            <list>
                <ref bean="studentOne"></ref>
                <ref bean="studentTwo"></ref>
                <ref bean="studentThree"></ref>
            </list>
        </property>-->
    </bean>

    <bean id="teacherOne" class="com.atguigu.spring.pojo.Teacher">
        <property name="tid" value="10086"></property>
        <property name="tname" value="大宝"></property>
    </bean>

    <bean id="teacherTwo" class="com.atguigu.spring.pojo.Teacher">
        <property name="tid" value="10010"></property>
        <property name="tname" value="小宝"></property>
    </bean>

    <!--配置一个集合类型的bean,需要使用util的约束-->
    <util:list id="studentList">
        <ref bean="studentOne"></ref>
        <ref bean="studentTwo"></ref>
        <ref bean="studentThree"></ref>
    </util:list>

    <util:map id="teacherMap">
        <entry key="10086" value-ref="teacherOne"></entry>
        <entry key="10010" value-ref="teacherTwo"></entry>
    </util:map>

    <bean id="studentSix" class="com.atguigu.spring.pojo.Student"
        p:sid="1005" p:sname="小明" p:teacherMap-ref="teacherMap"></bean>
</beans>

IOCByXMLTest

package com.atguigu.spring.test;

import com.atguigu.spring.pojo.Clazz;
import com.atguigu.spring.pojo.Person;
import com.atguigu.spring.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Date:2022/7/1
 * Author:ybc
 * Description:
 */
public class IOCByXMLTest {

    /**
     * 获取bean的三种方式:
     * 1、根据bean的id获取
     * 2、根据bean的类型获取
     * 注意:根据类型获取bean时,要求IOC容器中有且只有一个类型匹配的bean
     * 若没有任何一个类型匹配的bean,此时抛出异常:NoSuchBeanDefinitionException
     * 若有多个类型匹配的bean,此时抛出异常:NoUniqueBeanDefinitionException
     * 3、根据bean的id和类型获取
     * 结论:
     * 根据类型来获取bean时,在满足bean唯一性的前提下
     * 其实只是看:『对象 instanceof 指定的类型』的返回结果
     * 只要返回的是true就可以认定为和类型匹配,能够获取到。
     * 即通过bean的类型、bean所继承的类的类型、bean所实现的接口的类型都可以获取bean
     */

    @Test
    public void testDI(){
        //获取IOC容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
        //获取bean
        Student student = ioc.getBean("studentSix", Student.class);
        System.out.println(student);
        /*Clazz clazz = ioc.getBean("clazzInner", Clazz.class);
        System.out.println(clazz);*/
        /*Clazz clazz = ioc.getBean("clazzOne", Clazz.class);
        System.out.println(clazz);*/
    }
    @Test
    public void testIOC(){
        //获取IOC容器
        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
        //获取bean
        //Student studentOne = (Student) ioc.getBean("studentOne");
        //Student student = ioc.getBean(Student.class);
        //Student student = ioc.getBean("studentOne", Student.class);
        Person person = ioc.getBean(Person.class);
        System.out.println(person);
    }
}

使用util:list、util:map标签必须引入相应的命名空间,可以通过idea的提示功能选择

2.2.9、实验九:p命名空间

引入p命名空间后(需要导入相应的p约束-idea自动导入),可以通过以下方式为bean的各个属性赋值


和上面一样,如果是类类型则加ref引用bean id,字面量不用加


用的不多


在Spring中,p代表property,即Bean的属性。在XML配置文件中,使用p命名空间可以更方便地设置Bean的属性,使得XML配置更加简洁和易读。例如,使用p命名空间可以这样设置Bean的属性:

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

2.2.10、实验十:引入外部属性文件

①加入依赖

数据源就是来统一管理数据库连接的,所以必须有MySQL驱动


加入德鲁伊依赖



还有很多属性可以配置,都有默认值,也可以不设置


property中initialSize是设置数据库连接池中初始化为我们连接的个数(默认值是0)


maxActive是当前数据库连接池中最大能够存在连接的数量(默认值是8)


如果我们只从这个数据源中获取连接而不关闭,也就是不将这些连接返回,最多只能获取8个,如果获取第九个时会一直出现堵塞状态


maxWait等待分配连接的最大等待时间,如果不设置就一直等待

<!-- MySQL驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>
<!-- 数据源 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.31</version>
</dependency>


②创建外部属性文件



jdbc.user=root
jdbc.password=atguigu
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
③引入属性文件
  • 如果导入错了context,一定要删除相应的约束,再导入,不然可能找不到
<!-- 引入外部属性文件 ,每一个新的内容都需要引入新的约束,直接写resources下面的配置文件名-和配置文件一个目录,类路径-->
<context:property-placeholder location="classpath:jdbc.properties"/>
④配置bean
  • 接口连构造方法都没,是不能写接口的
  • 这里class写的是一个德鲁伊druid的实现类
<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>
⑤测试
@Test
public void testDataSource() throws SQLException {
    ApplicationContext ac = new ClassPathXmlApplicationContext("spring-datasource.xml");
    DataSource dataSource = ac.getBean(DataSource.class);
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
}

2.2.11、实验十一:bean的作用域

作用域(作用范围)

在IOC容器中,我们获取的这个bean的对象始终为单实例模式

①概念

在Spring中可以通过配置bean标签的scope属性来指定bean的作用域范围,各取值含义参加下表:

取值 含义 创建对象的时机
singleton(默认) 在IOC容器中,这个bean的对象始终为单实例 IOC容器初始化时
prototype 这个bean在IOC容器中有多个实例 获取bean时

如果是在WebApplicationContext环境下还会有另外两个作用域(但不常用):

取值 含义
request 在一个请求范围内有效
session 在一个会话范围内有效


②创建类User
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
    public User() {
    }
    public User(Integer id, String username, String password, Integer age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", age=" + age +
            '}';
    }
}
③配置bean
<!-- scope属性:取值singleton(默认值),bean在IOC容器中只有一个实例,IOC容器初始化时创建
对象 -->
<!-- scope属性:取值prototype,bean在IOC容器中可以有多个实例,getBean()时创建对象 -->
<bean class="com.atguigu.bean.User" scope="prototype"></bean>
④测试
  • 单例就是这个类有且只会有有一个实例对象,大家用的都是同一个
  • 设置了scope="prototype"(原型、多例的意思)
<bean class="com.atguigu.bean.User" scope="prototype"></bean>就不是单例了

@Test
public void testBeanScope(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("spring-scope.xml");
    User user1 = ac.getBean(User.class);
    User user2 = ac.getBean(User.class);
    System.out.println(user1==user2);//true--证明默认是单例模式,设置scope="prototype"就是false
}

2.2.12、实验十二:bean的生命周期

①具体的生命周期过程

bean对象创建(调用无参构造器实例化对象)


给bean对象设置属性


bean对象初始化之前操作(由bean的后置处理器负责)


bean对象初始化(需在配置bean时指定初始化方法:init-method属性)


bean对象初始化之后操作(由bean的后置处理器负责)


bean对象就绪可以使用


bean对象销毁(需在配置bean时指定销毁方法)


IOC容器关闭


生命周期


同上:重点摘要实例化(Instantiation):当Spring容器接收到一个Bean的定义时,会创建该Bean的一个实例。


属性赋值(Properties Setting):在Bean实例化后,Spring容器会根据Bean定义中的配置,将属性值或依赖注入到Bean中。


初始化(Initialization):在Bean属性赋值完成后,Spring容器会调用Bean的初始化方法,例如实现了InitializingBean接口的afterPropertiesSet()方法或者在Bean定义中配置的init-method方法。


使用(In Use):Bean初始化完成后,可以被应用程序使用。


销毁(Destruction):当应用程序关闭时,Spring容器会调用Bean的销毁方法,例如实现了DisposableBean接口的destroy()方法或者在Bean定义中配置的destroy-method方法。


disposable-disposable,adj.一次性的,用完即丢弃的;可支配的,可自由使用的;(人,观点)可有可无的,可轻易放弃的


n.一次性用品


IOC销毁的时候查到里面的bean还在,就销毁bean,然后把bean全部销毁完后才IOC容器才关闭

②修改类User


public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    public User() {
        System.out.println("生命周期:1、创建对象");
    }
    public User(Integer id, String username, String password, Integer age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        System.out.println("生命周期:2、依赖注入(也就是赋值-随便找一个属性演示了)");
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public void initMethod(){
        System.out.println("生命周期:3、初始化");
    }
    public void destroyMethod(){
        System.out.println("生命周期:5、销毁(4是使用,我们这里demo了)");
    }
    @Override
    public String toString() {
        return "User{" +
            "id=" + id +
            ", username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", age=" + age +
            '}';
    }
}

注意其中的initMethod() 和 destroyMethod(),可以通过配置bean指定为初始化和销毁的方法

③配置bean
  • 指定之后才可以被调用

注意destory用ApplicationContext不能被调用,需要更换IOC容器,有close的方法容器才可以(关闭时候才能摧毁)


用 ClassPathXmlApplicationContext或ConfigurableApplicationContext才有ioc.close方法(单例)

<!-- 使用init-method属性指定初始化方法 -->
<!-- 使用destroy-method属性指定销毁方法 -->
<bean class="com.atguigu.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>
④测试
@Test
public void testLife(){
    ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring-lifecycle.xml");
    User bean = ac.getBean(User.class);
    System.out.println("生命周期:4、通过IOC容器获取bean并使用");
    ac.close();
}

通过IOC容器获取bean时候,获取的永远都是唯一的一个对象,我们没必要在获取它的时候创建,在获取IOC容器时候就已把它给创建好,以后用的都是同一个


实例化,依赖注入,初始化在获取IOC容器时候就执行了,不是在获取bean的时候执行的)


IOC容器中配置的bean默认作用域是单例


如果设置bean中的scope为prototype多例(多例每一次获取对象都是新的对象,IOC没必要在创建容器时候就把它给创建好)则不会执行生命周期(在获取getBean的时候才会进行初始化一系列操作,输出以上内容-多例不会执行destory)


设置为多例的时候,它的destory方法就不由我们的IOC容器管理了(bean里面配置了摧毁也不会destory,会前三个生命周期)


解疑


同学你好,当设置Bean的作用域为prototype时,销毁方法会出现冲突。所以destroy方法没有被执行。


因为当作用域为prototype时,Spring IOC容器不能够对Bean的整个生命周期进行管理,最终对象的销毁和资源回收由使用者负责


按我理解的是,当作用域为prototype时,Spring容器不会Bean的整个生命周期进行管理,它只是负责new一个对象出来,当你使用完该对象后会自动销毁或者等待回收,如果作用域换成singleton,它的生命周期就由Spring容器负责

⑤bean的后置处理器

bean的后置处理器会在生命周期的初始化前后添加额外的操作,需要实现BeanPostProcessor接口,


且配置到IOC容器中,需要注意的是,bean后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行


创建bean的后置处理器:


默认方法不是抽象方法,所以不强制被重写。但是可以被重写,重写的时候去掉default关键字


idea -ctrl + o重写方法

package com.atguigu.spring.process;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
        throws BeansException {
        //此方法在bean的生命周期初始化之前执行
        System.out.println("☆☆☆" + beanName + " = " + bean);
        //所以这里返回的是bean额外操作之后的对象,不进行额外操作,直接返回 return bean也行,beanName就是bean的id
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
        throws BeansException {
        //
        System.out.println("★★★" + beanName + " = " + bean);
        return bean;
    }
}

在IOC容器中配置后置处理器:



bean后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行


因为我们直接把bean配置到了IOC容器中,不是针对于某一个bean(所有的都会生命周期都会加上)


2.2.13、实验十三:FactoryBean

①简介

前面我们讲过了BeanFactory 是BeanFactory而不是FactoryBean

BeanFactory就是IOC最基本的实现,帮我们管理Bean


FactoryBean是专门作为一个Bean交给IOC容器管理


BeanFactory 是 Spring IoC 容器的基础,用于管理 Bean 对象的创建和生命周期,而 FactoryBean 是一个特殊的 Bean,用于定制 Bean 的创建过程。


BeanFactory 可以管理多种类型的 Bean 对象,而 FactoryBean 只能创建一种特定类型的 Bean 对象。


BeanFactory 可以通过配置文件或注解来创建 Bean 对象,而 FactoryBean 可以在创建 Bean 之前进行一些初始化操作或返回一个代理对象。


CSDN:进一步理解- BeanFactory和FactoryBean的区别_factorybean和beanfactory的区别-CSDN博客


FactoryBean是Spring提供的一种整合第三方框架的常用机制。和普通的bean不同,配置一个


FactoryBean类型的bean,在获取bean的时候得到的并不是class属性中配置的这个类的对象,而是


getObject()方法的返回值。通过这种机制,Spring可以帮我们把复杂组件创建的详细过程和繁琐细节都屏蔽起来,只把最简洁的使用界面展示给我们。


如果我们配置FactoryBean到IOC容器中,虽然我们配置的是FactoryBean类型,但是我们获取时候,可以直接获取工厂所提供的对象


将来我们整合Mybatis时,Spring就是通过FactoryBean机制来帮我们创建SqlSessionFactory对象的。

/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory;
import org.springframework.lang.Nullable;
/**
* Interface to be implemented by objects used within a {@link BeanFactory}
which
* are themselves factories for individual objects. If a bean implements this
* interface, it is used as a factory for an object to expose, not directly as a
* bean instance that will be exposed itself.
*
* <p><b>NB: A bean that implements this interface cannot be used as a normal
bean.</b>
* A FactoryBean is defined in a bean style, but the object exposed for bean
* references ({@link #getObject()}) is always the object that it creates.
*
* <p>FactoryBeans can support singletons and prototypes, and can either create
* objects lazily on demand or eagerly on startup. The {@link SmartFactoryBean}
* interface allows for exposing more fine-grained behavioral metadata.
*
* <p>This interface is heavily used within the framework itself, for example
for
* the AOP {@link org.springframework.aop.framework.ProxyFactoryBean} or the
* {@link org.springframework.jndi.JndiObjectFactoryBean}. It can be used for
* custom components as well; however, this is only common for infrastructure
code.
*
* <p><b>{@code FactoryBean} is a programmatic contract. Implementations are not
* supposed to rely on annotation-driven injection or other reflective
facilities.</b>
* {@link #getObjectType()} {@link #getObject()} invocations may arrive early in
the
* bootstrap process, even ahead of any post-processor setup. If you need access
to
* other beans, implement {@link BeanFactoryAware} and obtain them
programmatically.
*
* <p><b>The container is only responsible for managing the lifecycle of the
FactoryBean
* instance, not the lifecycle of the objects created by the FactoryBean.</b>
Therefore,
* a destroy method on an exposed bean object (such as {@link
java.io.Closeable#close()}
* will <i>not</i> be called automatically. Instead, a FactoryBean should
implement
* {@link DisposableBean} and delegate any such close call to the underlying
object.
*
* <p>Finally, FactoryBean objects participate in the containing BeanFactory's
* synchronization of bean creation. There is usually no need for internal
* synchronization other than for purposes of lazy initialization within the
* FactoryBean itself (or the like).
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 08.03.2003
* @param <T> the bean type
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.aop.framework.ProxyFactoryBean
* @see org.springframework.jndi.JndiObjectFactoryBean
*/
public interface FactoryBean<T> {
    /**
* The name of an attribute that can be
* {@link org.springframework.core.AttributeAccessor#setAttribute set} on a
* {@link org.springframework.beans.factory.config.BeanDefinition} so that
* factory beans can signal their object type when it can't be deduced from
* the factory bean class.
* @since 5.2
*/
    String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
    /**
* Return an instance (possibly shared or independent) of the object
* managed by this factory.
* <p>As with a {@link BeanFactory}, this allows support for both the
* Singleton and Prototype design pattern.
* <p>If this FactoryBean is not fully initialized yet at the time of
* the call (for example because it is involved in a circular reference),
* throw a corresponding {@link FactoryBeanNotInitializedException}.
* <p>As of Spring 2.0, FactoryBeans are allowed to return {@code null}
* objects. The factory will consider this as normal value to be used; it
* will not throw a FactoryBeanNotInitializedException in this case anymore.
* FactoryBean implementations are encouraged to throw
* FactoryBeanNotInitializedException themselves now, as appropriate.
* @return an instance of the bean (can be {@code null})
* @throws Exception in case of creation errors
* @see FactoryBeanNotInitializedException
*/
    @Nullable
    T getObject() throws Exception;
    /**
* Return the type of object that this FactoryBean creates,
* or {@code null} if not known in advance.
* <p>This allows one to check for specific types of beans without
* instantiating objects, for example on autowiring.
* <p>In the case of implementations that are creating a singleton object,
* this method should try to avoid singleton creation as far as possible;
* it should rather estimate the type in advance.
* For prototypes, returning a meaningful type here is advisable too.
* <p>This method can be called <i>before</i> this FactoryBean has
* been fully initialized. It must not rely on state created during
* initialization; of course, it can still use such state if available.
* <p><b>NOTE:</b> Autowiring will simply ignore FactoryBeans that return
* {@code null} here. Therefore it is highly recommended to implement
* this method properly, using the current state of the FactoryBean.
* @return the type of object that this FactoryBean creates,
* or {@code null} if not known at the time of the call
* @see ListableBeanFactory#getBeansOfType
*/
    @Nullable
    Class<?> getObjectType();
    /**
* Is the object managed by this factory a singleton? That is,
* will {@link #getObject()} always return the same object
* (a reference that can be cached)?
* <p><b>NOTE:</b> If a FactoryBean indicates to hold a singleton object,
* the object returned from {@code getObject()} might get cached
* by the owning BeanFactory. Hence, do not return {@code true}
* unless the FactoryBean always exposes the same reference.
* <p>The singleton status of the FactoryBean itself will generally
* be provided by the owning BeanFactory; usually, it has to be
* defined as singleton there.
* <p><b>NOTE:</b> This method returning {@code false} does not
* necessarily indicate that returned objects are independent instances.
* An implementation of the extended {@link SmartFactoryBean} interface
* may explicitly indicate independent instances through its
* {@link SmartFactoryBean#isPrototype()} method. Plain {@link FactoryBean}
* implementations which do not implement this extended interface are
* simply assumed to always return independent instances if the
* {@code isSingleton()} implementation returns {@code false}.
* <p>The default implementation returns {@code true}, since a
* {@code FactoryBean} typically manages a singleton instance.
* @return whether the exposed object is a singleton
* @see #getObject()
* @see SmartFactoryBean#isPrototype()
*/
    //证明FactoryBean是单例
    default boolean isSingleton() {
        return true;
    }
}
②创建类UserFactoryBean
public class UserFactoryBean implements FactoryBean<User> {
    @Override
    public User getObject() throws Exception {
        return new User();
    }
    @Override
    public Class<?> getObjectType() {
        return User.class;
    }
}

我们实现FactoryBean时候它默认让我们实现两个方法,证明另外的一定有一个默认方法

③配置bean
  • 不需要配置User了
  • 不需要配置普通的工厂
  • 当我们加载IOC配置文件去获取IOC容器时候,它其实是把UserFactoryBean中的getObject()方法所返回的对象给了IOC容器
  • 这样可以达到不配做User,却能够使用User(FactoryBean的getObject方法所返回的对象交给ioc容器)


<bean id="user" class="com.atguigu.bean.UserFactoryBean"></bean>
④测试
  • 会发现实例化,并没有依赖注入为我们属性赋值,所以生命周期2初始化并没有

@Test
public void testUserFactoryBean(){
    //获取IOC容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("spring-factorybean.xml");
     User user = ac.getBean(User.class);
    System.out.println(user);
}
public class UserFactoryBean implements FactoryBean<User> {
    @Override
    public User getObject() throws Exception {
        return new User();
    }

    @Override
    public Class<?> getObjectType() {
        return User.class;
    }
}

FactoryBean是一个接口,需要创建一个类实现该接口

  • 其中有三个方法:
  • getObject():通过一个对象交给IOC容器管理
  • getObjectType():设置所提供对象的类型
  • isSingleton():所提供的对象是否单例
  • 当把FactoryBean的实现类配置为bean时,会将当前类中getObject()所返回的对象交给IOC容器管理


BeanFactory和FactoryBean的区别


BeanFactory是Spring框架的核心接口之一,而FactoryBean是一个接口。


BeanFactory负责创建和管理Spring容器中的对象,而FactoryBean是用于创建其他Bean实例的特殊Bean。


当我们在配置文件中定义一个FactoryBean时,实际上创建的是FactoryBean本身,而不是它所创建的对象。

使用场景

BeanFactory适用于以下场景:


需要轻量级的Spring容器,不需要使用Spring提供的大部分扩展功能,只需要基本的Bean容器功能。


需要手动控制Bean的创建和销毁,例如在某些特定的场景下需要动态地创建和销毁Bean实例。


需要定制Bean的生命周期,例如需要在Bean实例化之前或销毁之后进行特定的处理。


FactoryBean适用于以下场景:


需要创建复杂的Bean实例,例如需要根据不同的条件创建不同的Bean实例。


需要在Bean实例化之前或销毁之后进行特定的处理,例如需要在Bean实例化之前进行一些预处理操作。


需要对Bean实例进行代理,例如需要为Bean实例添加事务或安全控制等功能。


需要在Spring容器中动态地创建Bean实例,例如需要在应用程序运行时根据某些条件创建Bean实例。


在实际开发中,我们可以根据具体的需求选择BeanFactory或FactoryBean来创建和管理Bean实例。


优质全套Spring全套教程二https://developer.aliyun.com/article/1491439



相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
14天前
|
XML JavaScript Java
Spring Retry 教程
Spring Retry 是 Spring 提供的用于处理方法重试的库,通过 AOP 提供声明式重试机制,不侵入业务逻辑代码。主要步骤包括:添加依赖、启用重试机制、设置重试策略(如异常类型、重试次数、延迟策略等),并可定义重试失败后的回调方法。适用于因瞬时故障导致的操作失败场景。
Spring Retry 教程
|
2月前
|
Java 数据库连接 Spring
一文讲明 Spring 的使用 【全网超详细教程】
这篇文章是一份全面的Spring框架使用教程,涵盖了从基础的项目搭建、IOC和AOP概念的介绍,到Spring的依赖注入、动态代理、事务处理等高级主题,并通过代码示例和配置文件展示了如何在实际项目中应用Spring框架的各种功能。
一文讲明 Spring 的使用 【全网超详细教程】
|
3月前
|
NoSQL Java 数据库连接
《滚雪球学Spring Boot》教程导航帖(更新于2024.07.16)
📚 《滚雪球学Spring Boot》是由CSDN博主bug菌创作的全面Spring Boot教程。作者是全栈开发专家,在多个技术社区如CSDN、掘金、InfoQ、51CTO等担任博客专家,并拥有超过20万的全网粉丝。该教程分为入门篇和进阶篇,每篇包含详细的教学步骤,涵盖Spring Boot的基础和高级主题。
51 1
《滚雪球学Spring Boot》教程导航帖(更新于2024.07.16)
|
2月前
|
SQL Java 数据库连接
Spring Boot联手MyBatis,打造开发利器:从入门到精通,实战教程带你飞越编程高峰!
【8月更文挑战第29天】Spring Boot与MyBatis分别是Java快速开发和持久层框架的优秀代表。本文通过整合Spring Boot与MyBatis,展示了如何在项目中添加相关依赖、配置数据源及MyBatis,并通过实战示例介绍了实体类、Mapper接口及Controller的创建过程。通过本文,你将学会如何利用这两款工具提高开发效率,实现数据的增删查改等复杂操作,为实际项目开发提供有力支持。
61 0
|
2月前
|
Java 关系型数据库 MySQL
|
4月前
|
存储 NoSQL Java
教程:Spring Boot与RocksDB本地存储的整合方法
教程:Spring Boot与RocksDB本地存储的整合方法
|
3月前
|
Java 索引 Spring
教程:Spring Boot中集成Elasticsearch的步骤
教程:Spring Boot中集成Elasticsearch的步骤
|
3月前
|
Java API Spring
教程:Spring Boot中如何集成GraphQL
教程:Spring Boot中如何集成GraphQL
|
3月前
|
缓存 Java Spring
教程:Spring Boot中集成Memcached的详细步骤
教程:Spring Boot中集成Memcached的详细步骤
|
4月前
|
搜索推荐 Java 索引
教程:Spring Boot中集成Elasticsearch的步骤
教程:Spring Boot中集成Elasticsearch的步骤