为了账号安全,请及时绑定邮箱和手机立即绑定

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万
获赞与收藏
906

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消