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. J2EE分布式架构及MySQL交流群

    J2EE分布式架构及MySQL交流群:577913057

  2. [转]MVC、MVP、MVVM

    界面之下:还原真实的 MVC.MVP.MVVM 模式 [日期:2015-10-28] 来源:github.com/livoras  作者:戴嘉华 [字体:大 中 小]   前言 做客户端开发.前端开发 ...

  3. JavaScript基础15——js的DOM对象

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 解决maven项目update project会更改jdk版本问题

    一.问题描述             在Eclipse中新建了一个Maven工程, 然后更改JDK版本为1.6, 结果每次使用Maven > Update project的时候JDK版本都恢复成 ...

  5. ASP.NET MVC 微信公共平台开发之获取用户消息并处理

    ASP.NET MVC 微信公共平台开发 获取用户消息并处理 获取用户消息 用户发送的消息是在微信服务器发送的一个HTTP POST请求中包含的,获取用户发送的消息要从POST请求的数据流中获取 微信 ...

  6. Web.Config的配置

    1.配置数据库连接 在<connectionStrings></connectionStrings>节中完成,配置过程需指定四个属性server(DataSource)服务器名 ...

  7. CSS3选择器(一)

    E[att^='val'] 选择属性值以val开头的任何字符 E[att$='val'] 选择属性值以val结尾的任何字符 E[att*='val'] 选择属性值包含val的任何字符 :root HT ...

  8. DataTable 转换为ArrayList 再转换成 json 格式

    //JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();        //javaScriptSeriali ...

  9. 在R语言环境中无法载入rJava包的解决办法

    问题描述: 安装包xlsx包后,运行library("xlsx")后弹出错误窗口: RGui (64-bit): Rgui.exe - 系统错误 无法启动此程序,因为计算机中丢失 ...

  10. (转)解决ScrollView嵌套ListView或者GridView导致只显示一行的方法

    即动态获取ListView和GridView的高度 一.对于ListView ListView listview= new ListView(this); setListViewHeightBased ...