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();
}

github地址:https://github.com/leechenxiang/maven-spring001-helloworld

Spring 一二事(4) - 单例的更多相关文章

  1. Spring容器-ApplicationContext的单例设计

    Spring容器-ApplicationContext的单例设计   每次通过new创建一个ApplicationContext容器,都会执行refresh方法,看源代码了解到这个refresh方法会 ...

  2. SSM-Spring-05:Spring的bean是单例还是多例,怎么改变?

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- Spring的bean是单例的 它在Spring容器初始化的时候创建对象 可以修改为多例,在此bean节点中添 ...

  3. 【Spring】8、Spring框架中的单例Beans是线程安全的么

    看到这样一个问题:spring框架中的单例Beans是线程安全的么? Spring框架并没有对单例bean进行任何多线程的封装处理.关于单例bean的线程安全和并发问题需要开发者自行去搞定.但实际上, ...

  4. Spring5源码解析-Spring框架中的单例和原型bean

    Spring5源码解析-Spring框架中的单例和原型bean 最近一直有问我单例和原型bean的一些原理性问题,这里就开一篇来说说的 通过Spring中的依赖注入极大方便了我们的开发.在xml通过& ...

  5. Spring 的Controller 是单例or多例

    Spring 的Controller 是单例or多例 你什么也不肯放弃,又得到了什么? 背景:今天写代码遇到一个Controller 中的线程安全问题,那么Spring 的Controller 是单例 ...

  6. Spring框架中的单例bean是线程安全的吗?

    不,Spring框架中的单例bean不是线程安全的.

  7. Spring 框架中的单例 bean 是线程安全的吗?

    不,Spring 框架中的单例 bean 不是线程安全的.

  8. Spring注解【非单例】

    花了至少一整天的时间解决了这个问题,必须记录这个纠结的过程,问题不可怕,思路很绕弯. 为了能说清楚自己的问题,我都用例子来模拟. 我有一个类MyThread是这样的: @Service public ...

  9. Spring框架中的单例Beans是线程安全的么?

    Spring框架并没有对单例bean进行任何多线程的封装处理.关于单例bean的线程安全和并发问题需要开发者自行去搞定.但实际上,大部分的Spring bean并没有可变的状态(比如Serview类和 ...

随机推荐

  1. 清除浮动after

    .clearf{display: inline-block;} .clearf:after { content: "."; display: block; height:; cle ...

  2. 从" ThinkPHP 开发规范 "看 PHP 的命名规范和开发建议

    稍稍水一篇博客,摘抄自Think PHP 的开发规范,很有引导性,我们可以将这些规范实践到原生 PHP 中. 命名规范 使用ThinkPHP开发的过程中应该尽量遵循下列命名规范: 类文件都是以.cla ...

  3. datepicker冲突

    公司里的项目由于发展较快,很多东西都没有好好梳理一下,以至于有很多的潜在的问题. 最近就遇到了一个比较坑的问题.datepicker 有两个插件库中的datepicker插件比较有名.一个是jQuer ...

  4. RHEL7软件包管理

    本文介绍RHEL7的软件包管理 RHEL7下主要有RPM和YUM这两种包管理: YUM使用简单但需要联网,YUM会去网上的YUM包源去获取所需要的软件包并获取该包依赖的其他包 RPM的需要的操作精度比 ...

  5. CKEditor与CKFinder的配置

    CKEditor与CKFinder的配置使用(一) 将CKEditor 与 CKFinder 的包含在项目中 从http://cksource.com网站上下载CKEditor与CKFinder,并将 ...

  6. 模拟Select-Options对象实现多项数据输入功能

       模拟Select-Options对象实现多项数据输入功能 Select-Options对象可以同时输入多项值并将所输入数据存入内表以供程序使用,不过Select-Options的功能有一定的局限 ...

  7. Microsoft Dynamics CRM 2013 CD-KEY

    Microsoft Dynamics CRM Workgroup Server 2013 (5 CAL limit):NX77Y-BTBCV-JP3T3-8W7JH-94QJP Microsoft D ...

  8. This application is currently offline. To enable the application, remove the app_offline.htm file from the application root directory.

    IIS提示:This application is currently offline. To enable the application, remove the app_offline.htm f ...

  9. pycharm安装与实践

    PyCharm简介 PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成. ...

  10. UIlable 属性详用

    我的好朋友给我制定了一个新的学习方法,从新的看每个控件,去了解他的每个属性,方法来让自己对oc的认识更加充实 今天重新认识一下UILable 的属性lable的阴影设置: 阴影的偏移量是以lable中 ...