前言
此前的面向对象设计思想说到,要面对抽象编程,不要面向具体编程,依赖至于抽象等,具体的有依赖倒置原则、里氏替换原则等等,而Spring核心思想DI和IOC就是对面向对象设计的一个实践。
Spring框架(在Java中称为IoC容器)。在该框架中,通过XML配置文件建立接口和具体类之间的关系,IoC容器通过该配置文件来做具体的新建的工作,这样在实际应用中,只需要修改配置文件就能换成不同的具体类,从而不需要修改任何代码了。
下面让我们来看看Spring IOC容器管理的bean的生命周期
bean生命周期
栗子
maven依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.4</version>
</dependency>
<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
测试类
package com.shiqitool.async.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;
/**
* @program: shiqitool
* @description:
* @author: shiqi
* @create: 2024-09-06 16:03
**/
@Component
public class SpringLifeBean implements BeanDefinitionRegistryPostProcessor, BeanFactoryPostProcessor, InstantiationAwareBeanPostProcessor,
MergedBeanDefinitionPostProcessor, InitializingBean,
BeanNameAware, BeanFactoryAware, BeanClassLoaderAware, ApplicationContextAware, ApplicationEventPublisherAware, ImportAware {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
System.out.println("======postProcessBeanDefinitionRegistry===========");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("======postProcessBeanFactory===========");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("======postProcessBeforeInitialization===========");
System.out.println(beanName);
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("======postProcessAfterInitialization===========");
System.out.println(beanName);
return null;
}
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition rootBeanDefinition, Class<?> aClass, String s) {
System.out.println("======postProcessMergedBeanDefinition===========");
System.out.println(s);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("======afterPropertiesSet=======================");
}
@Override
public void setBeanName(String s) {
System.out.println("================BeanNameAware=====================");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("================BeanFactoryAware=====================");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("================BeanClassLoaderAware=====================");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("======ApplicationContextAware=========================");
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
System.out.println("======ApplicationEventPublisherAware=========================");
}
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
System.out.println("======ImportAware=========================");
}
}
启动Application
================BeanNameAware=====================
================BeanClassLoaderAware=====================
================BeanFactoryAware=====================
======ApplicationEventPublisherAware=========================
======ApplicationContextAware=========================
======afterPropertiesSet=======================
======postProcessBeanDefinitionRegistry===========
======postProcessBeanFactory===========
======postProcessMergedBeanDefinition===========
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat
======postProcessBeforeInitialization===========
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat
======postProcessAfterInitialization===========
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryConfiguration$EmbeddedTomcat
结论:
先执行aware接口、后afterPropertiesSet、后postProcessBeanDefinitionRegistry、postProcessBeanFactory(只执行一次)
创建bean实例的时候postProcessMergedBeanDefinition、postProcessBeforeInitialization、初始化、postProcessAfterInitialization会执行多次