GeezerVison is showing github.com — :sparkles: Introducing new features.Spring lifecycle · sustbeckham/Java-Interview@ebffa51
File tree
Expand file treeCollapse file tree
Open diff view settings
src/main/java/com/crossoverjie/spring/processor
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: MD/spring/spring-bean-lifecycle.md +16-1Lines changed: 16 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -115,14 +115,22 @@ public class SpringLifeCycleAware implements ApplicationContextAware { |
115 | 115 |
|
116 | 116 | 这样在 `springLifeCycleAware` 这个 bean 初始化会就会调用 `setApplicationContext` 方法,并可以获得 `applicationContext` 对象。 |
117 | 117 |
|
118 | | -### BeanPostProcessor 后处理器 |
| 118 | +### BeanPostProcessor 增强处理器 |
119 | 119 |
|
120 | 120 | 实现 BeanPostProcessor 接口,Spring 中所有 bean 在做初始化时都会调用该接口中的两个方法,可以用于对一些特殊的 bean 进行处理: |
121 | 121 |
|
122 | 122 | ```java |
123 | 123 | @Component |
124 | 124 | public class SpringLifeCycleProcessor implements BeanPostProcessor { |
125 | 125 | private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleProcessor.class); |
| 126 | + |
| 127 | + /** |
| 128 | + * 预初始化 初始化之前调用 |
| 129 | + * @param bean |
| 130 | + * @param beanName |
| 131 | + * @return |
| 132 | + * @throws BeansException |
| 133 | + */ |
126 | 134 | @Override |
127 | 135 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
128 | 136 | if ("annotationBean".equals(beanName)){ |
@@ -131,6 +139,13 @@ public class SpringLifeCycleProcessor implements BeanPostProcessor { |
131 | 139 | return bean; |
132 | 140 | } |
133 | 141 |
|
| 142 | + /** |
| 143 | + * 后初始化 bean 初始化完成调用 |
| 144 | + * @param bean |
| 145 | + * @param beanName |
| 146 | + * @return |
| 147 | + * @throws BeansException |
| 148 | + */ |
134 | 149 | @Override |
135 | 150 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
136 | 151 | if ("annotationBean".equals(beanName)){ |
|
Collapse file
Copy file name to clipboardExpand all lines: src/main/java/com/crossoverjie/spring/processor/SpringLifeCycleProcessor.java +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
17 | 17 | @Component |
18 | 18 | public class SpringLifeCycleProcessor implements BeanPostProcessor { |
19 | 19 | private final static Logger LOGGER = LoggerFactory.getLogger(SpringLifeCycleProcessor.class); |
| 20 | + |
| 21 | + /** |
| 22 | + * 预初始化 初始化之前调用 |
| 23 | + * @param bean |
| 24 | + * @param beanName |
| 25 | + * @return |
| 26 | + * @throws BeansException |
| 27 | + */ |
20 | 28 | @Override |
21 | 29 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
22 | 30 | if ("annotationBean".equals(beanName)){ |
@@ -25,6 +33,13 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro |
25 | 33 | return bean; |
26 | 34 | } |
27 | 35 |
|
| 36 | + /** |
| 37 | + * 后初始化 bean 初始化完成调用 |
| 38 | + * @param bean |
| 39 | + * @param beanName |
| 40 | + * @return |
| 41 | + * @throws BeansException |
| 42 | + */ |
28 | 43 | @Override |
29 | 44 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
30 | 45 | if ("annotationBean".equals(beanName)){ |
|