2021-07-05

Spring中Bean初始化和销毁方法执行的优先级

Spring有三对初始化和销毁方法

  1. 通过@Bean注解指定initMethod和destroyMethod
  2. 实现InitializingBean和DisposableBean接口
  3. 使用@PostContruct和@PreDestroy

那么问题来了,这三个执行的优先级是什么呢?

定义一个Car类

public class Car implements InitializingBean, DisposableBean { public Car() {  System.out.println("car constructor"); } @PostConstruct public void postConstruct(){  System.out.println("postConstruct"); } @Override public void afterPropertiesSet() throws Exception {  System.out.println("InitializingBean afterPropertiesSet"); } public void initMethod(){  System.out.println("initMethod"); } @PreDestroy public void preDestroy(){  System.out.println("preDestroy"); } @Override public void destroy(){  System.out.println("DisposableBean destroy"); } public void destroyMethod(){  System.out.println("destroyMethod"); }}

定义配置类,并注入

@Configurationpublic class MyConfigOfLifeCycle { @Bean(initMethod = "initMethod",destroyMethod = "destroyMethod") public Car car(){  return new Car(); }}

测试

@Testpublic void test04(){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfigO......

原文转载:http://www.shaoqun.com/a/848254.html

跨境电商:https://www.ikjzd.com/

盘古集团:https://www.ikjzd.com/w/1448

camel:https://www.ikjzd.com/w/331.html

gem:https://www.ikjzd.com/w/1997


Spring有三对初始化和销毁方法通过@Bean注解指定initMethod和destroyMethod实现InitializingBean和DisposableBean接口使用@PostContruct和@PreDestroy那么问题来了,这三个执行的优先级是什么呢?定义一个Car类publicclassCarimplementsInitializingBean,DisposableBean{p
vincent:https://www.ikjzd.com/w/1642
大卖经验分享:MyMALL平台运营秘诀及俄罗斯电商市场痛点:https://www.ikjzd.com/articles/22026
亚马逊春季大促中我们该如何去蹭好这波流量,巩固销量。:https://www.ikjzd.com/articles/22027
看90后企二代美女,如何书写纺织产业新时代?:https://www.ikjzd.com/articles/22029
亚马逊品牌分析上新功能 / DHL在非洲推出电商购物APP:https://www.ikjzd.com/articles/22030
老头一边吃奶一边摸下面 被老头做的死去活来:http://lady.shaoqun.com/a/247872.html
把班主任玩到怀孕 老师奶头好大,下面好多水水:http://lady.shaoqun.com/m/a/248298.html
上课忘穿内裤被男同桌摸 上课把女同学玩出水了:http://www.30bags.com/m/a/249793.html
Tik Tok开疆拓土,正在不断刺激体育行业!:https://www.ikjzd.com/articles/146373
7月1日起,从英国发送到欧盟的运送方式有重大变化!:https://www.ikjzd.com/articles/146367
跨境电商前沿观察--7月:https://www.ikjzd.com/articles/146363
一个优秀的亚马逊卖家每天在做些什么呢?:https://www.ikjzd.com/articles/146357

No comments:

Post a Comment