亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Spring 一二事(4) - 單例

標簽:
Java

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認證。主要從事后端技術和架構領域,有豐富的電商平臺與物流平臺核心系統的架構設計和開發經驗。
評論

作者其他優質文章

正在加載中
全棧工程師
手記
粉絲
1.2萬
獲贊與收藏
931

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消