前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >InitializingBean 与 CommandLineRunner

InitializingBean 与 CommandLineRunner

原创
作者头像
用户3293499
发布2024-08-13 13:15:31
790
发布2024-08-13 13:15:31
举报
文章被收录于专栏:Spring杂谈

InitializingBean 与 CommandLineRunner(或者ApplicationRunner) 两个接口里面都提供了启动后调用的抽象方法用于我们进行拓展。日常使用时,两者皆可。

代码语言:java
复制
@FunctionalInterface
public interface CommandLineRunner extends Runner {

	/**
	 * Callback used to run the bean.
	 * @param args incoming main method arguments
	 * @throws Exception on error
	 */
	void run(String... args) throws Exception;

}
代码语言:java
复制
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;

}

两者的区别如下:

1、调用时机 CommandLineRunner 要晚于 InitializingBean 调用

2、InitializingBean 在bean初始化后调用,而CommandLineRunner是在整个环境初始化完成后调用

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档