欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring的bean生命周期

程序员文章站 2022-06-15 10:28:32
spring中bean的生命周期一、单实例和多实例:1、创建单实例:在容器启动的时候创建对象多实例:在每次获取的时候创建对象2、初始化对象创建完成,并赋值好,调用初始化方法3、销毁单实例:容器关闭的时候多实例:容器不会管理这个bean,容器不会调用销毁方法;二、自定义1、指定初始化和销毁方法@Configurationpublic class MainConfigOfLifeCycle { @Bean(initMethod = "init",destroyMethod...

spring中bean的生命周期

一、单实例和多实例:

1、创建

单实例:在容器启动的时候创建对象
多实例:在每次获取的时候创建对象

2、初始化

对象创建完成,并赋值好,调用初始化方法

3、销毁

单实例:容器关闭的时候
多实例:容器不会管理这个bean,容器不会调用销毁方法;

二、自定义

1、指定初始化和销毁方法
@Configuration public class MainConfigOfLifeCycle { @Bean(initMethod = "init",destroyMethod = "destroy") public Car car(){ return new Car(); } } 
2、让Bean实现InitializingBean,DisposableBean
@Component public class Car implements InitializingBean, DisposableBean { public Car(){ System.out.println("car constructor"); } @Override public void destroy() throws Exception { System.out.println("car ... destroy "); } /**
     * 初始化init方法()
     * @throws Exception
     */ @Override public void afterPropertiesSet() throws Exception { System.out.println("car......afterPropertiesSet....."); } } 九月 28, 2020 11:49:52 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@38cccef: startup date [Mon Sep 28 11:49:52 CST 2020]; root of context hierarchy
九月 28, 2020 11:49:53 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'car' with a different definition: replacing [Generic bean: class [com.zqs.bean.Car]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\ideaWork\spring-annotaion\target\classes\com\zqs\bean\Car.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=mainConfigOfLifeCycle; factoryMethodName=car; initMethodName=null; destroyMethodName=(inferred); defined in com.zqs.config.MainConfigOfLifeCycle] car constructor
car......afterPropertiesSet..... 容器创建完成。。。。
九月 28, 2020 11:49:53 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@38cccef: startup date [Mon Sep 28 11:49:52 CST 2020]; root of context hierarchy
car ... destroy 

Process finished with exit code 0 
3、使用注解 @PostConstruct、@PreDestroy注解
import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @Component public class Car { public Car(){ System.out.println("car constructor"); } @PreDestroy public void destroy() throws Exception { System.out.println("car ... PreDestroy(销毁) "); } /**
     * 初始化init方法()
     * @throws Exception
     */ @PostConstruct public void init() { System.out.println("car......PostConstruct(初始化)....."); } } 九月 28, 2020 4:43:22 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'car' with a different definition: replacing [Generic bean: class [com.zqs.bean.Car]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\ideaWork\spring-annotaion\target\classes\com\zqs\bean\Car.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=mainConfigOfLifeCycle; factoryMethodName=car; initMethodName=null; destroyMethodName=(inferred); defined in com.zqs.config.MainConfigOfLifeCycle] car constructor
car......PostConstruct(初始化)..... 容器创建完成。。。。
九月 28, 2020 4:43:22 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@38cccef: startup date [Mon Sep 28 16:43:21 CST 2020]; root of context hierarchy
car ... PreDestroy(销毁) Process finished with exit code 0 
4、使用BeanPostProcessor(接口)后处理器(主要是对象初始化前后对对象的处理)

重写两方法

postProcessBeforeInitialization() 初始化之前调用

postProcessAfterInitialization() 初始化之后调用

@Component public class Car { public Car(){ System.out.println("car constructor"); } @PreDestroy public void destroy() throws Exception { System.out.println("car ... PreDestroy(销毁) "); } /**
     * 初始化init方法()
     * @throws Exception
     */ @PostConstruct public void init() { System.out.println("car......PostConstruct(初始化)....."); } } @Component public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("postProcessBeforeInitialization....."+s ); return o; } @Override public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("postProcessAfterInitialization....."+s ); return o; } } 信息: Overriding bean definition for bean 'car' with a different definition: replacing [Generic bean: class [com.zqs.bean.Car]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\ideaWork\spring-annotaion\target\classes\com\zqs\bean\Car.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=mainConfigOfLifeCycle; factoryMethodName=car; initMethodName=null; destroyMethodName=(inferred); defined in com.zqs.config.MainConfigOfLifeCycle] postProcessBeforeInitialization.....org.springframework.context.event.internalEventListenerProcessor
postProcessAfterInitialization.....org.springframework.context.event.internalEventListenerProcessor
postProcessBeforeInitialization.....org.springframework.context.event.internalEventListenerFactory
postProcessAfterInitialization.....org.springframework.context.event.internalEventListenerFactory
postProcessBeforeInitialization.....mainConfigOfLifeCycle
postProcessAfterInitialization.....mainConfigOfLifeCycle
car constructor
postProcessBeforeInitialization.....car
car......PostConstruct(初始化)..... postProcessAfterInitialization.....car
容器创建完成。。。。

Process finished with exit code 0 

本文地址:https://blog.csdn.net/weixin_49161209/article/details/108842734

相关标签: Spring bean