【Spring的Bean的生命周期】

目录

1.实例化Bean对象

Bean的实例化是在BeanFactory中完成的。BeanFactory通过调用Bean的构造函数来创建Bean的实例,此时还未进行任何属性注入。
源码:

//从beanDefinition中获取Bean的class
Class<?> beanClass = resolveBeanClass(beanDefinition, beanName);
//根据构造函数实例化Bean对象
Object beanInstance = createBeanInstance(beanName, beanDefinition, args);

实例代码:

 // 使用容器
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }

}

没有整合SpringBoot之前:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//读取配置文件并实例化Bean
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Object bean = context.getBean("beanId");

2.处理Bean的属性依赖

在实例化Bean对象之后,Spring容器会将按照实例化时配置的属性进行依赖注入,也就是将其他的Bean对象注入进来,这一步也是整个生命周期中比较重要的一步。

populateBean(beanName, beanDefinition, instanceWrapper);

例如:

@Component("userService")
public class UserServiceImpl implements BeanNameAware, UserService {

    @Autowied
    private OrderService orderService;
}

UserServiceImpl 需要依赖OrderService ,因此在UserServiceImpl 实例化之后,Spring容器会自动将OrderService 对象注入进来。
源码:

3.Bean的初始化

初始化是在BeanFactory中完成的,包括调用InitializingBeanafterPropertiesSet()方法或@PostConstruct注解标注的方法等。

if (mbd.getInitMethodName() != null || mbd.getDestroyMethodName() != null) {
    return createLifecycleProcessor().initializeBean(beanName, exposedObject, mbd);
}

3.1 前置处理

如果Bean实现了BeanPostProcessor接口,则在Bean的初始化之前调用postProcessBeforeInitialization()方法。这是BeanPostProcessor的前置处理,可以对Bean进行任何必要的修改。

3.2 后置处理

如果Bean实现了BeanPostProcessor接口,则在Bean的初始化之后调用postProcessAfterInitialization()方法。这是BeanPostProcessor的后置处理,可以对Bean进行任何必要的修改。

4.Bean在容器中的使用

Bean在容器中的使用包括:getBean@Autowire注解等。当容器使用某个Bean时,Bean实例已经存在并且初始化完毕。

5.Bean的销毁

Bean的销毁可以通过实现DisposableBean接口的其中一个方法destroy()实现,也可以通过@PreDestroy注解标注的方法实现。由于Spring的IoC容器是单例模式的,所以在容器的销毁过程中会调用单例Bean的销毁方法。

源码:

if (mbd.getDestroyMethodName() != null) {
    registerDisposableBeanIfNecessary(beanName, bean, mbd);
}

小结:

较为重要的在2、3步。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring生命周期包括实例化、配置和销毁三个阶段。首先,实例化阶段是指创建一个Bean的实例。在Spring中,Bean的实例化可以通过使用BeanFactory或ApplicationContext来获取。其次,配置阶段是指对实例化的Bean进行配置,也就是进行IOC注入。在这个阶段,Spring会根据配置文件中的Bean的id值进行相应的配置。如果Bean实现了BeanNameAware接口,Spring还会调用它实现的setBeanName(String)方法,传递的参数就是Bean的id值。最后,销毁阶段是指当Bean不再使用时进行垃圾回收。对于Singleton模式的Bean,Spring会负责管理整个生命周期;而对于Prototype模式的Bean,Spring在创建好并交给使用者后就不再管理后续的生命周期。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [关于Spring Bean的生命周期](https://blog.csdn.net/Apeopl/article/details/82964799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Springbean生命周期详解](https://blog.csdn.net/qq_64169170/article/details/123052663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值