日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

销毁Bean的基本操作有哪些?

發(fā)布時(shí)間:2024/4/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 销毁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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。