Spring的bean的注创建、依赖注入、自动装配

本文详细介绍了Spring框架中的bean管理,包括bean的作用域(Singleton和Prototype)、生命周期、以及bean之间的关系(继承、依赖和引用)。接着讲解了bean的实例化方法,如构造方法、静态工厂、实例工厂和FactoryBean。然后,重点阐述了依赖注入的多种方式,如setter注入、构造器注入、集合属性注入和命名空间注入,以及SpEL表达式。最后,讨论了bean的自动装配模式,如constructor、byName和byType,帮助理解Spring如何简化对象之间的依赖关系。
摘要由CSDN通过智能技术生成

一、bean

定义:被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的。bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。

bean作用域
  • Singleton:是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。你可以在 bean 的配置文件中设置作用域的属性为 singleton,如下所示:
    在这里插入图片描述
<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
</bean>
  • Prototype:是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。
Bean的生命周期
  • Bean的生命周期可以表达为:Bean的定义——Bean的初始化——Bean的使用——Bean的销毁
public class Lion {
    public static void main(String[] args) {
    }
    private String name ;
    public Lion() {
        super();
        System.out.println( "public Lion()" );
    }
    public void init(){
        System.out.println( "public void init()" );
    }
    public void destory(){
        System.out.println( "public void destory()" );
    }
    }
XML配置文件中如下:

  <bean id="lion" class="vip.ycpower.ioc.lifecycle.Lion"
          p:name="辛巴"
          init-method="init"
          destroy-method="destory"
          scope="singleton"/>
bean的关系

-继承关系

  • 依赖关系
  • 引用关系
继承
<bean id="parent" class="vip.ycpower.ioc.relation.Foreigner"
         p:type="美国人"
          p:lastName="唐纳德"/>
          
    <bean id="child" class="vip.ycpower.ioc.relation.Foreigner"
          p:firstName="特朗普"
          parent="parent"/>
依赖
public class Engine {
    public void init(){
        System.out.println( "[ Engine ] [ public void init() ]" );
    }

    public void start(){
        System.out.println( "[ Engine ] [ 引擎启动 ]" );
    }
}
public class Plane {
    private Engine engine;
 
    public void init(){
        System.out.println( "[ Plane ] [ public void init() ]" );
    }

    public void fly(){
        engine.start();
        System.out.println( "[ Plane ] [ 灰机起灰了 ]" );
    }
配置文件
<bean id="plane" class="vip.ycpower.ioc.relation.Plane" init-method="init" autowire="byName"
          depends-on="engine"/>

    <bean id="engine" class="vip.ycpower.ioc.relation.Engine" init-method="init"/>
引用
<bean id="tz" class="java.util.TimeZone" factory-method="getDefault"/>

    <bean id="timezoneUtil" class="vip.ycpower.ioc.relation.TimezoneUtil">
        <property name="timezoneId">
            <!-- 使用 idref 用于引用 一个 已经存在 的 bean 的 名称 -->
          <idref bean="tz"/>
        </property>

二、bean的实例化

1、 通过一个构造方法来实例化 Bean
public class Human {

    private Integer id ;
    private String name ;
    private char gender ;
    private Date birthdate ;
    private boolean married ;

    public Human() {
        super();
        System.out.println( "public Human()" );
    }

    public Human(Integer id, String name, char gender) {
        super();
        System.out.println( "public Human(Integer,String,char)" );
        this.id = id;
        this.name = name;
        this.gender = gender;
    }

    @Override
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值