国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring與bean有關(guān)的生命周期示例詳解

Spring與bean有關(guān)的生命周期示例詳解

2020-07-27 12:55eaglelihh Java教程

這篇文章主要給大家介紹了關(guān)于Spring與bean有關(guān)的生命周期的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Spring具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

前言

記得以前的時候,每次提起Spring中的bean相關(guān)的生命周期時,內(nèi)心都無比的恐懼,因為好像有很多,自己又理不清楚:什么beanFactory啊,aware接口啊,beanPostProcessor啊,afterPropertiesSet啊,initMethod啊等等。

今天終于理清這些關(guān)系了,并且又新增了對postConstruct和lifecycle的理解。

執(zhí)行順序

- 首先是 BeanFactoryPostProcessor,它是針對所有bean的definition的,只執(zhí)行一次

下面是針對每個bean的初始

  • - 實現(xiàn)了一系列aware接口的,比如BeanNameAware,ApplicationContextAware,調(diào)用其set方法
  • - 執(zhí)行BeanPostProcessor的postProcessBeforeInitialization方法
  • - 帶有@PostConstruct注解的方法
  • - 實現(xiàn)InitializingBean接口的afterPropertiesSet方法
  • - 指定的initMethod方法
  • - 執(zhí)行BeanPostProcessor的postProcessAfterInitialization方法
  • - 實現(xiàn)了SmartLifecycle接口的start方法(實現(xiàn)Lifecycle接口的不會自動調(diào)用,需要顯式的調(diào)用start方法)

下面是針對每個bean的銷毀

  • - 實現(xiàn)了SmartLifecycle接口的stop方法(實現(xiàn)Lifecycle接口的不會自動調(diào)用,需要顯式的調(diào)用stop方法)
  • - 帶有@PreDestroy注解的方法
  • - 實現(xiàn)DisposableBean接口的destroy方法
  • - 指定的destroyMethod方法

目前就想到這么多了,其他的麻煩在評論區(qū)留言呀~

代碼實例

bean實體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * @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
 * 實現(xiàn)了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
 * 實現(xiàn)了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;
  }
}

配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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");
  }
}

結(jié)果說明

結(jié)果正如前面所說的執(zhí)行順序一致,主要注意的就是Lifecycle接口和SmartLifecycle接口,只有實現(xiàn)了SmartLifecycle接口的bean在初始化時才會被自動調(diào)用,而實現(xiàn)了Lifecycle接口的除非顯式調(diào)用start和stop方法才會被調(diào)用。

總結(jié)

到此這篇關(guān)于Spring與bean有關(guān)的生命周期的文章就介紹到這了,更多相關(guān)Spring與bean生命周期內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://www.cnblogs.com/eaglelihh/p/13383039.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产资源在线看 | 黄色片在线看 | 久久精品国产一区二区三区 | 懂色av成人一区二区三区 | 免费看黄色一级电影 | 51国产午夜精品免费视频 | 亚洲精品久久久 | 日日夜夜国产 | 色999国产| 一区二区三区久久久久久 | 性激烈欧美三级在线播放狩猎 | 毛片一区 | 亚洲一区二区在线 | 日韩精品免费在线视频 | 成人毛片在线观看视频 | 欧美一区二区三区精品 | 久久国| 免费观看av电影 | 国产一区二区av | 精品国产乱码久久久久久密桃99 | 午夜视频免费在线观看 | 国产精品久久久久久模特 | 久久久免费 | 中文在线一区二区 | 亚洲精品久久久久久国 | 国产精品一区二区无线 | 国产中文字幕一区 | 国产精品久久久久久吹潮 | 日产欧产va高清 | 国产精品久久久久久久久久99 | 亚洲综合无码一区二区 | 夜夜av | 亚洲狠狠丁香婷婷综合久久久 | 国产成人一区二区三区在线观看 | 日韩欧美的一区二区 | 亚洲欧美日韩另类精品一区二区三区 | 亚洲精品99| 一区免费视频 | 4438x成人网最大色成网站 | 欧美在线不卡视频 | 丝袜+亚洲+另类+欧美+变态 |