您好,登录后才能下订单哦!
这篇文章主要讲解了Spring中的bean相关的生命周期,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
前言
记得以前的时候,每次提起Spring中的bean相关的生命周期时,内心都无比的恐惧,因为好像有很多,自己又理不清楚:什么beanFactory啊,aware接口啊,beanPostProcessor啊,afterPropertiesSet啊,initMethod啊等等。
今天终于理清这些关系了,并且又新增了对postConstruct和lifecycle的理解。
执行顺序
- 首先是 BeanFactoryPostProcessor,它是针对所有bean的definition的,只执行一次
下面是针对每个bean的初始
下面是针对每个bean的销毁
目前就想到这么多了,其他的麻烦在评论区留言呀~
代码实例
bean实体类
/** * @date: 2020-07-22 * * 一个简单的枚举类 */ public enum BeanType { NORMAL, LIFECYCLE, SMART_LIFECYCLE; } import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * @author: lihui * @date: 2020-07-22 * 一个简单的bean */ @Slf4j public class NormalBean implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean { private BeanType beanType; public NormalBean() { this(BeanType.NORMAL); } public NormalBean(BeanType beanType) { this.beanType = beanType; } @PostConstruct public void postConstruct() { log.info("{}, postConstruct", beanType); } @Override public void afterPropertiesSet() throws Exception { log.info("{}, afterPropertiesSet", beanType); } public void initMethod() { log.info("{}, initMethod", beanType); } @PreDestroy public void preDestroy() { log.info("{}, preDestroy", beanType); } @Override public void destroy() throws Exception { log.info("{}, destroy", beanType); } public void destroyMethod() { log.info("{}, destroyMethod", beanType); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { log.info("setApplicationContext, applicationContext : {}", applicationContext); } @Override public void setBeanName(String name) { log.info("setBeanName, bean name : {}", name); } } import lombok.extern.slf4j.Slf4j; import org.springframework.context.Lifecycle; /** * @author: lihui * @date: 2020-07-22 * 实现了Lifecycle的一个bean */ @Slf4j public class LifecycleBean extends NormalBean implements Lifecycle { private volatile boolean running = false; public LifecycleBean() { super(BeanType.LIFECYCLE); } @Override public void start() { log.info("start"); running = true; } @Override public void stop() { log.info("stop"); running = false; } @Override public boolean isRunning() { return running; } } import lombok.extern.slf4j.Slf4j; import org.springframework.context.SmartLifecycle; /** * @author: lihui * @date: 2020-07-22 * 实现了SmartLifecycle的一个bean */ @Slf4j public class SmartLifecycleBean extends NormalBean implements SmartLifecycle { private volatile boolean running = false; public SmartLifecycleBean() { super(BeanType.SMART_LIFECYCLE); } @Override public void start() { log.info("start"); running = true; } @Override public void stop() { log.info("stop"); running = false; } @Override public boolean isRunning() { return running; } }
配置类
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; /** * @author: lihui * @date: 2020-07-25 */ @Slf4j public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { log.info("postProcessBeanFactory, beanFactory:{}", beanFactory); } } import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * @author: lihui * @date: 2020-07-25 */ @Slf4j public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { log.info("postProcessBeforeInitialization, bean:{}", beanName); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { log.info("postProcessAfterInitialization, bean:{}", beanName); return bean; } } import lombok.extern.slf4j.Slf4j; import org.springframework.context.SmartLifecycle; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author: lihui * @date: 2020-07-22 */ @Configuration @Slf4j public class Config { @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod") public NormalBean normalBean() { return new NormalBean(); } @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod") public LifecycleBean lifecycleBean() { return new LifecycleBean(); } @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod") public SmartLifecycle smartLifecycle() { return new SmartLifecycleBean(); } @Bean public static MyBeanFactoryPostProcessor myBeanFactoryPostProcessor() { return new MyBeanFactoryPostProcessor(); } @Bean public static MyBeanPostProcessor myBeanPostProcessor() { return new MyBeanPostProcessor(); } }
Main类
import lombok.extern.slf4j.Slf4j; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author: lihui * @date: 2020-07-22 */ @Slf4j public class Main { public static void main(String[] args) throws InterruptedException { ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class); ctx.registerShutdownHook(); Thread.sleep(5000); log.info("line ----------------------------- line"); ctx.start(); ctx.stop(); log.info("line ----------------------------- line"); } }
结果说明
结果正如前面所说的执行顺序一致,主要注意的就是Lifecycle接口和SmartLifecycle接口,只有实现了SmartLifecycle接口的bean在初始化时才会被自动调用,而实现了Lifecycle接口的除非显式调用start和stop方法才会被调用。
看完上述内容,是不是对Spring中的bean相关的生命周期有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。