spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session
在配置spring mvc的action时,可以对action使用 prototype
<!-- singleton: 默认单例 prototype: 多例 request ,session和global session: 只适用于web程序 -->
<bean id="scope_prototype" class="com.lee.spring005.scope.Scope"
scope="prototype"></bean>
<!-- destroy-method 一般在数据源的时候用到,关闭容易后就销毁连接 -->
<bean id="initDestory" class="com.lee.spring006.init_destory.InitDestory"
init-method="init" destroy-method="destory"></bean>
bean的创建销毁过程:
package com.lee.spring006.init_destory;
public class InitDestory {
public InitDestory() {
System.out.println("InitDestory() ");
}
public void hello() {
System.out.println("Hello InitDestory!");
}
public void init() {
System.out.println("init");
}
public void destory() {
System.out.println("destory");
}
}
测试:
@Test
public void testInitDestory() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
InitDestory initDestory = (InitDestory)context.getBean("initDestory");
initDestory.hello();
ClassPathXmlApplicationContext app = (ClassPathXmlApplicationContext)context;
app.close();
}
點擊查看更多內容
風間影月說
去圍觀
創業公司技術總監, 10年+開發和技術管理經驗。SUN認證SCJP、PMP、MCP認證。主要從事后端技術和架構領域,有豐富的電商平臺與物流平臺核心系統的架構設計和開發經驗。
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少


贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦