Skip to content

Commit 8d99cb7

Browse files
committed
✨ Introducing new features.Spring lifecycle
1 parent e9ab450 commit 8d99cb7

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

MD/spring/spring-bean-lifecycle.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class AnnotationBean {
3636

3737
### InitializingBean, DisposableBean 接口
3838

39-
还可以实现 `InitializingBean,DisposableBean` 这两个接口,也是在初始化已经销毁阶段调用
39+
还可以实现 `InitializingBean,DisposableBean` 这两个接口,也是在初始化以及销毁阶段调用
4040

4141
```java
4242
@Service
@@ -56,7 +56,7 @@ public class SpringLifeCycleService implements InitializingBean,DisposableBean{
5656

5757
### 自定义初始化和销毁方法
5858

59-
也可以自定义方法用于在初始化、销毁阶段进行调用:
59+
也可以自定义方法用于在初始化、销毁阶段调用:
6060

6161
```java
6262
@Configuration
@@ -85,7 +85,7 @@ public class SpringLifeCycle{
8585
}
8686
```
8787

88-
以上是在 SpringBoot 中可以这样配置,如果是原始的基于 XML 也是可以利用:
88+
以上是在 SpringBoot 中可以这样配置,如果是原始的基于 XML 也是可以使用:
8989

9090
```
9191
<bean class="com.crossoverjie.spring.SpringLifeCycle" init-method="start" destroy-method="destroy">
@@ -113,23 +113,29 @@ public class SpringLifeCycleAware implements ApplicationContextAware {
113113
}
114114
```
115115

116-
这样在 `springLifeCycleAware` 这个 bean 初始化会就会调用 `setApplicationContext` 方法,并可以获得 applicationContext 对象。
116+
这样在 `springLifeCycleAware` 这个 bean 初始化会就会调用 `setApplicationContext` 方法,并可以获得 `applicationContext` 对象。
117117

118118
### BeanPostProcessor 后处理器
119119

120-
也可以实现 BeanPostProcessor 接口,Spring 中所有 bean 在做初始化时都会调用该接口中的两个方法,可以用于对一些特殊的 bean 进行处理:
120+
实现 BeanPostProcessor 接口,Spring 中所有 bean 在做初始化时都会调用该接口中的两个方法,可以用于对一些特殊的 bean 进行处理:
121121

122122
```java
123123
@Component
124124
public class SpringLifeCycleProcessor implements BeanPostProcessor {
125125
private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleProcessor.class);
126126
@Override
127127
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
128+
if ("annotationBean".equals(beanName)){
129+
LOGGER.info("SpringLifeCycleProcessor start beanName={}",beanName);
130+
}
128131
return bean;
129132
}
130133

131134
@Override
132135
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
136+
if ("annotationBean".equals(beanName)){
137+
LOGGER.info("SpringLifeCycleProcessor end beanName={}",beanName);
138+
}
133139
return bean;
134140
}
135141
}

0 commit comments

Comments
 (0)