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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 一二事(4) - 单例

發布時間:2025/3/20 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 一二事(4) - 单例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

spring bean配置后再默認情況下是單例的,如果需要配置可以選擇?prototype, request, session和global session

在配置spring mvc的action時,可以對action使用?prototype

1    <!-- singleton: 默認單例 prototype: 多例 request ,session和global session: 只適用于web程序 --> 2 <bean id="scope_prototype" class="com.lee.spring005.scope.Scope" 3 scope="prototype"></bean> 4 5 <!-- destroy-method 一般在數據源的時候用到,關閉容易后就銷毀連接 --> 6 <bean id="initDestory" class="com.lee.spring006.init_destory.InitDestory" 7 init-method="init" destroy-method="destory"></bean>

bean的創建銷毀過程:

1 package com.lee.spring006.init_destory; 2 3 public class InitDestory { 4 5 public InitDestory() { 6 System.out.println("InitDestory() "); 7 } 8 9 public void hello() { 10 System.out.println("Hello InitDestory!"); 11 } 12 13 public void init() { 14 System.out.println("init"); 15 } 16 17 public void destory() { 18 System.out.println("destory"); 19 } 20 }

測試:

1 @Test 2 public void testInitDestory() { 3 4 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 5 6 InitDestory initDestory = (InitDestory)context.getBean("initDestory"); 7 initDestory.hello(); 8 9 ClassPathXmlApplicationContext app = (ClassPathXmlApplicationContext)context; 10 app.close(); 11 }

?github地址:https://github.com/leechenxiang/maven-spring001-helloworld

轉載于:https://www.cnblogs.com/leechenxiang/p/5305377.html

總結

以上是生活随笔為你收集整理的Spring 一二事(4) - 单例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。