Spring的refresh方法-InitializingBean番外篇(4

所有的内容都是基于 https://gitee.com/chris_777/spring-boot2.1.3 springboot 源码
main函数入口org.springframework.boot.tests.hibernate52.Hibernate52Application.main


InitializingBean根据名字就能判断出来是跟初始化有关系的,Spring会在获取Bean方法(org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean),对象初始化(org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean)阶段,调用Bean的init方法时候,会判断当前的Bean是否为InitializingBean类型,则执行相应的逻辑。

下面就是InitializingBean接口的具体代码,只有一个afterPropertiesSet方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface InitializingBean {

/**
* Invoked by the containing {@code BeanFactory} after it has set all bean properties
* and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
* <p>This method allows the bean instance to perform validation of its overall
* configuration and final initialization when all bean properties have been set.
* @throws Exception in the event of misconfiguration (such as failure to set an
* essential property) or if initialization fails for any other reason
*/
void afterPropertiesSet() throws Exception;

}

需要结合具体业务实现相应的场景,但是我目前想不到相应场景,那就输出一个hello word吧

1
2
3
4
5
6
7
8
9
10
@Component
public class MyInitializingBeanTest implements InitializingBean {

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("hello word InitializingBean");
}

}
//启动输出 hello word InitializingBean
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信