【Spring】Spring常用配置-Bean的初始化和销毁(生命周期)
发布日期:2021-06-29 13:38:40
浏览次数:2
分类:技术文章
本文共 2873 字,大约阅读时间需要 9 分钟。
转载请注明出处:
本文源自【】
分析
在我们实际开发的时候,经常会遇到在Bean使用之前或者之后做些必要的操作,Spring对Bean的生命周期的操作提供了支持。
有如下2种方式:
1、Java配置方式:使用@Bean的initMethod和destroyMethod(相当于xml配置的init-method和destroy-method)2、注解方式:
利用JSR-250的@PostConstruct和@PreDestroy友情提示:
进行本示例的演示,需要先配置好Maven和Spring哦、 见:示例
增加JSR250支持
javax.annotation jsr250-api 1.0
使用@Bean形式的Bean
package cn.hncu.p2_3_2BeanLifecycle;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/14. * Time: 上午 11:19. * Explain:使用@Bean形式的Bean */public class BeanWayService { public BeanWayService() { super(); System.out.println("初始化构造函数-BeanWayService:"+this.getClass()); } public void init(){ System.out.println("BeanWayService-init方法"); } public void destroy(){ System.out.println("BeanWayService-destroy方法"); }}
使用JSR250形式的Bean
package cn.hncu.p2_3_2BeanLifecycle;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/14. * Time: 上午 11:23. * Explain:使用JSR250形式的Bean */public class JSR250WayService { public JSR250WayService() { super(); System.out.println("初始化构造函数-JSR250WayService:"+this.getClass()); } @PostConstruct//@PostConstruct这个注解表明该方法在构造函数执行之后执行 public void init(){ System.out.println("jsr250-init方法"); } @PreDestroy//这个注解表明该方法在Bean销毁之前执行 public void destroy(){ System.out.println("jsr250-destroy方法"); }}
配置类
package cn.hncu.p2_3_2BeanLifecycle;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/14. * Time: 上午 11:29. * Explain:配置类 */@Configuration@ComponentScan("cn.hncu.p2_3_2BeanLifecycle")public class PrePostConfig { //为BeanWayService准备的配置方法 //initMethod和destroyMethod指定BeanWayService类的init和destroy方法在构造之后、Bean销毁之前执行 @Bean(initMethod = "init",destroyMethod = "destroy") BeanWayService beanWayService(){ return new BeanWayService(); } //为JSR250WayService准备的配置方法 @Bean JSR250WayService jsr250WayService(){ return new JSR250WayService(); }}
运行类
package cn.hncu.p2_3_2BeanLifecycle;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * Created with IntelliJ IDEA. * User: 陈浩翔. * Date: 2016/11/14. * Time: 上午 11:33. * Explain:运行类 */public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PrePostConfig.class); BeanWayService beanWayService = context.getBean(BeanWayService.class); JSR250WayService jsr250WayService = context.getBean(JSR250WayService.class); context.close(); }}
运行结果
转载请注明出处:
本文源自【】
转载地址:https://chenhx.blog.csdn.net/article/details/53162392 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月28日 09时44分51秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
买卖股票的最佳时机
2019-04-29
AUC粗浅理解笔记记录
2019-04-29
torch 模型运行时间与forward没对应的可能原因
2019-04-29
JavaScript 的addEventListener() 事件监听详解!
2019-04-29
上传图片到阿里云OSS和获取上传图片的url的详解 !
2019-04-29
Kafka为什么这么快?
2019-04-29
Java 生产者和消费者面试题
2019-04-29
生产者消费者问题
2019-04-29
本机电脑连接虚拟机redis失败解决方法
2019-04-29
DM365 应用层gpio控制
2019-04-29
linux i2c子系统abc
2019-04-29
CSS3 帧动画(Sprite,直译叫雪碧图)
2019-04-29
Java 父线程与子线程相互通信的方法
2019-04-29
Redis 六种淘汰策略和三种删除策略
2019-04-29
Java LinkedHashMap
2019-04-29
JPA 多线程同时对一条数据进行Update的问题
2019-04-29
JPA 多线程对数据进行更新,Update和Insert同时存在的问题
2019-04-29
Java 高性能队列Disruptor
2019-04-29
SpringBoot 使用https
2019-04-29
Java 读写锁
2019-04-29