从0开始学Spring二:Bean

一、前言

上一篇文章中,我们简单的介绍了Spring,同时提出了Spring中的一个核心模块IOC容器,同时我们也知道了SpringIOC容器中管理的是Bean对象,今天我们就接着上一篇文章继续学Spring中的Bean。

二、Bean

1、Spring Bean简介

Spring官方参考文档
在前一篇文中已经介绍了什么是Bean,其实就是一些被spring容器所管理的对象。这里借用一下Spring官网的一张图。这张图展示了我们自己写的业务对象是如何变成Spring中的Bean。

image.png


根据官网的介绍,我们的Bean最终在SpringIOC容器中会被保证成为BeanDeFinition的对象。一个BeanDeFinition对象包含

  • Bean的完整类名(带包名)
  • Bean的生命周期
  • Bean的依赖项
  • 创建Bean的其他参数,例如数据库Bean时需要配置相关参数

这些元数据转换为组成每个bean定义的一组属性

image.png


2、Bean的属性

当我们使用XML来配置Bean的时候通常会设置许多的属性值,接下来我们就介绍一些常用的属性
(1)ID:在Spring中每个Bean都有ID,通常来说一个Bean只有一个ID(你也可以不设置,如果不设置的话Spring会根据内置的算法来替我们生成Bean),在SpringIOC容器中,ID必须唯一。这也很好理解,只有唯一的ID才能取出唯一的对象,例如这样我们并不写ID,依然可以获取到People对象

<bean  class="com.cmxy.entity.People">
  <property name="name" value="hardy"/>
  <property name="age" value="27"/>
</bean>

image.png


(2)name:名称。Spring还给我们提供了name属性,用于给Bean设置名称。Spring中允许一个Bean有多个别名,多个别名之间用逗号隔开,如果没有设置ID只设置了别名,Spring会把第一个别名作为ID赋给我们写的Bean

<bean  class="com.cmxy.entity.People" name="people,people2,people3">
  <property name="name" value="hardy"/>
  <property name="age" value="27"/>
</bean>

(3)scope:这个属性值用于设置bean的作用域,认是singleton(单例),意思是在整个spring容器中只有一份额;还有一个prototype,每次调用都要新建一个Bean实例。
(tips:这里说的只有两种是基于Spring环境,没有web环境。如果web环境在还有 request、Session这里暂时不做讨论)

3、Bean的初始化

Typically, to specify the bean class to be constructed in the case where the container itself directly creates the bean by calling its constructor reflectively, somewhat equivalent to Java code with the new operator.To specify the actual class containing the static factory method that is invoked to create the object, in the less common case where the container invokes a static factory method on a class to create the bean. The object type returned from the invocation of the static factory method may be the same class or another class entirely.

译文:通常,在容器本身通过反射性地调用构造函数来直接创建bean的情况下,指定要构造的bean类,这在某种程度上相当于使用新操作符的Java代码。 要指定包含用于创建对象的静态工厂方法的实际类,在较不常见的情况下,容器调用类上的静态工厂方法来创建bean。调用静态工厂方法返回的对象类型可以是同一个类,也可以完全是另一个类。

public class People {

    private String name;

    private Integer age;

    public People(String name, Integer age) {
        this.name = name;
        this.age = age;
        System.out.println("通过构造函数创建对象");
    }
}

1、通过构造函数创建对象

可以看到Spring使用了对象的构造函数来创建对象

    <bean class="com.cmxy.entity.People" name="people,people2,people3">
        <constructor-arg value="hardy"/>
        <constructor-arg value="18"/>
    </bean>

image.png


2、通过静态工厂创建对象

<bean id="people" class="com.cmxy.entity.People" factory-method="createPeople">
    
</bean>
在Bean中指定工厂方法,用于创建对象
@Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class People{

        private String name;

        private Integer age;

        public static People createPeople(){
            return new People("hardy",27);
        }
    }

image.png


3、通过实例工厂创建对象

image.png

public class PeopleFactory {
    public People createPeople() {
        return new People("hardy", 27);
    }
}

image.png


三、结束语

今天的文章相对来说内容比较少,也比较简单。接下来的文章会继续分析Spring中的Bean,包括BeanDeFinition等等,希望对你有所帮助。

未完待续

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...
win11本地账户怎么改名?win11很多操作都变了样,用户如果想要...