SpringBoot 第四篇 Bean的生命周期

Spring Bean 的生命周期大致分为:Bean定义,Bean初始化,Bean的生存期,Bean的销毁四个部分

 a. 资源定位: 

         Spring 通过我们的配置 如@ComponentScan 定义扫描路径去找带有@Component的类,

         这个过程就是一个资源定位的过程。

b.Bean定义:

           一旦找到了资源,他就开始解析,并将信息保存起来。注意此时还没有初始化Bean,

            也就没有Bean实例,它有的仅仅是Bean的定义。然后将Bean定义发送到IOC容器中。

             此时依然只有定义。

c. 初始化:

             经过上述步骤,spring 会继续完成Bean 实例化和依赖注入。

            如果不想让spring继续初始化,可在@ComponentScan中设置lazyInit =true(默认为false)

          示例如下:

package com.didispace.service.impl;

import com.didispace.service.Animal;
import com.didispace.service.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class BussinessPerson implements Person {

    private Animal animal = null;


    @Override
    public void service() {
        this.animal.use();
    }

    @Override
    @Autowired @Qualifier("dog")
    public void setAnimal(Animal animal) {
        System.out.println("延迟依赖注入!!!");
        this.animal = animal;
    }
}
<
  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值