销毁Bean的基本操作有哪些?
生活随笔
收集整理的這篇文章主要介紹了
销毁Bean的基本操作有哪些?
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public class DefaultUserFactory implements UserFactory, DisposableBean {@PreDestroypublic void preDestroy() {System.out.println("@PreDestroy : UserFactory 銷毀中...");}@Overridepublic void destroy() throws Exception {System.out.println("DisposableBean#destroy() : UserFactory 銷毀中...");}public void doDestroy() {System.out.println("自定義銷毀方法 doDestroy() : UserFactory 銷毀中...");}@Overridepublic void finalize() throws Throwable {System.out.println("當(dāng)前 DefaultUserFactory 對象正在被垃圾回收...");}
}
@Configuration // Configuration Class
public class BeanInitializationDemo {public static void main(String[] args) {// 創(chuàng)建 BeanFactory 容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();// 注冊 Configuration Class(配置類)applicationContext.register(BeanInitializationDemo.class);// 啟動(dòng) Spring 應(yīng)用上下文applicationContext.refresh();// 非延遲初始化在 Spring 應(yīng)用上下文啟動(dòng)完成后,被初始化System.out.println("Spring 應(yīng)用上下文已啟動(dòng)...");// 依賴查找 UserFactoryUserFactory userFactory = applicationContext.getBean(UserFactory.class);System.out.println(userFactory);System.out.println("Spring 應(yīng)用上下文準(zhǔn)備關(guān)閉...");// 關(guān)閉 Spring 應(yīng)用上下文applicationContext.close();System.out.println("Spring 應(yīng)用上下文已關(guān)閉...");}@Bean(initMethod = "initUserFactory", destroyMethod = "doDestroy")@Lazy(value = false)public UserFactory userFactory() {return new DefaultUserFactory();}
}
protected void doClose() {// Check whether an actual close attempt is necessary...if (this.active.get() && this.closed.compareAndSet(false, true)) {if (logger.isDebugEnabled()) {logger.debug("Closing " + this);}LiveBeansView.unregisterApplicationContext(this);try {// Publish shutdown event.publishEvent(new ContextClosedEvent(this));}catch (Throwable ex) {logger.warn("Exception thrown from ApplicationListener handling ContextClosedEvent", ex);}// Stop all Lifecycle beans, to avoid delays during individual destruction.if (this.lifecycleProcessor != null) {try {this.lifecycleProcessor.onClose();}catch (Throwable ex) {logger.warn("Exception thrown from LifecycleProcessor on context close", ex);}}// Destroy all cached singletons in the context's BeanFactory.destroyBeans();// Close the state of this context itself.closeBeanFactory();// Let subclasses do some final clean-up if they wish...onClose();// Reset local application listeners to pre-refresh state.if (this.earlyApplicationListeners != null) {this.applicationListeners.clear();this.applicationListeners.addAll(this.earlyApplicationListeners);}// Switch to inactive.this.active.set(false);}
}
protected void destroyBeans() {getBeanFactory().destroySingletons();
}
public void destroySingletons() {super.destroySingletons();this.updateManualSingletonNames(Set::clear, (set) -> {return !set.isEmpty();});this.clearByTypeCache();
}
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PreDestroy {
}
?
總結(jié)
以上是生活随笔為你收集整理的销毁Bean的基本操作有哪些?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Xml 的IOC 容器-解析配置文件
- 下一篇: 单体 Bean 注册实例