當前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
Spring 一二事(4) - 单例
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                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) - 单例的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 一些当前 Node.js 中最流行 ES
- 下一篇: HDU1011 Starship Tro
