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();
}
點(diǎn)擊查看更多內(nèi)容
風(fēng)間影月說(shuō)
去圍觀
創(chuàng)業(yè)公司技術(shù)總監(jiān), 10年+開(kāi)發(fā)和技術(shù)管理經(jīng)驗(yàn)。SUN認(rèn)證SCJP、PMP、MCP認(rèn)證。主要從事后端技術(shù)和架構(gòu)領(lǐng)域,有豐富的電商平臺(tái)與物流平臺(tái)核心系統(tǒng)的架構(gòu)設(shè)計(jì)和開(kāi)發(fā)經(jīng)驗(yàn)。
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少


贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦