Spring
- InitializingBean
void afterPropertiesSet() throws Exception;
Invoked by a BeanFactory after it has set all bean properties supplied
当bean中的参数都初始化完毕后调用此方法
- DisposableBean
/**
* Interface to be implemented by beans that want to release resources
* on destruction. A BeanFactory is supposed to invoke the destroy
* method if it disposes a cached singleton. An application context
* is supposed to dispose all of its singletons on close.
*
* <p>An alternative to implementing DisposableBean is specifying a custom
* destroy-method, for example in an XML bean definition.
* For a list of all bean lifecycle methods, see the BeanFactory javadocs.
*
* @author Juergen Hoeller
* @since 12.08.2003
* @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName
* @see org.springframework.context.ConfigurableApplicationContext#close
*/
public interface DisposableBean {
/**
* Invoked by a BeanFactory on destruction of a singleton.
* @throws Exception in case of shutdown errors.
* Exceptions will get logged but not rethrown to allow
* other beans to release their resources too.
*/
void destroy() throws Exception;
}
当bean销毁的时候调用,用来做一些release resource
springboot
- ApplicationRunner
/**
* Callback used to run the bean.
* @param args incoming application arguments
* @throws Exception on error
*/
void run(ApplicationArguments args) throws Exception;
ApplicationArguments是SpringBoot中的一个接口,他的实现类是DefaultApplicationArguments
org.springframework.boot.DefaultApplicationArguments
org.springframework.boot.ApplicationArguments
Provides access to the arguments that were used to run a {@link SpringApplication}.
通过ApplicationArguments的注释可以看到通过ApplicationArguments获取SpringApplication启动的一些参数
- CommandLineRunner
/**
* Callback used to run the bean.
* @param args incoming main method arguments
* @throws Exception on error
*/
void run(String... args) throws Exception;
可以看出这两个接口的区别是run方法的参数,如果不需要获取SpringApplication的启动参数,可以直接使用CommandLineRunner
否者使用ApplicationRunner
总结
ApplicationRunner,CommandLineRunner是在容器初始化完毕后调用相应的方法
InitializingBean 在bean中的参数已经初始化完毕后调用afterPropertiesSet方法