1.InitializingBean与DisposableBean
InitializingBean
定义初始化逻辑,用于执行自定义初始化或者校验已设置的属性值等。
* Interface to be implemented by beans that need to react once all their properties * have been set by a {@link BeanFactory}: e.g. to perform custom initialization, * or merely to check that all mandatory properties have been set.
DisposableBean
定义销毁逻辑,用于一个实例bean销毁后需要释放资源等。 只有bean从容器中移除或者销毁或容器关闭时,才会调用该方法。
ApplicationContext关闭时,默认对所有单例bean都会调用这个方法。
* Interface to be implemented by beans that want to release resources on destruction. * A {@link BeanFactory} will invoke the destroy method on individual destruction of a * scoped bean. An {@link org.springframework.context.ApplicationContext} is supposed * to dispose all of its singletons on shutdown, driven by the application lifecycle.
2.实例
//实现InitializingB