Spring 一二事(4) - 单例
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) - 单例的更多相关文章
- Spring容器-ApplicationContext的单例设计
Spring容器-ApplicationContext的单例设计 每次通过new创建一个ApplicationContext容器,都会执行refresh方法,看源代码了解到这个refresh方法会 ...
- SSM-Spring-05:Spring的bean是单例还是多例,怎么改变?
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- Spring的bean是单例的 它在Spring容器初始化的时候创建对象 可以修改为多例,在此bean节点中添 ...
- 【Spring】8、Spring框架中的单例Beans是线程安全的么
看到这样一个问题:spring框架中的单例Beans是线程安全的么? Spring框架并没有对单例bean进行任何多线程的封装处理.关于单例bean的线程安全和并发问题需要开发者自行去搞定.但实际上, ...
- Spring5源码解析-Spring框架中的单例和原型bean
Spring5源码解析-Spring框架中的单例和原型bean 最近一直有问我单例和原型bean的一些原理性问题,这里就开一篇来说说的 通过Spring中的依赖注入极大方便了我们的开发.在xml通过& ...
- Spring 的Controller 是单例or多例
Spring 的Controller 是单例or多例 你什么也不肯放弃,又得到了什么? 背景:今天写代码遇到一个Controller 中的线程安全问题,那么Spring 的Controller 是单例 ...
- Spring框架中的单例bean是线程安全的吗?
不,Spring框架中的单例bean不是线程安全的.
- Spring 框架中的单例 bean 是线程安全的吗?
不,Spring 框架中的单例 bean 不是线程安全的.
- Spring注解【非单例】
花了至少一整天的时间解决了这个问题,必须记录这个纠结的过程,问题不可怕,思路很绕弯. 为了能说清楚自己的问题,我都用例子来模拟. 我有一个类MyThread是这样的: @Service public ...
- Spring框架中的单例Beans是线程安全的么?
Spring框架并没有对单例bean进行任何多线程的封装处理.关于单例bean的线程安全和并发问题需要开发者自行去搞定.但实际上,大部分的Spring bean并没有可变的状态(比如Serview类和 ...
随机推荐
- PHP redis Api 中文文档
phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...
- Android 手机卫士15--程序锁
1.基本思路 ①.创建已加锁应用的数据库(字段:_id,packagename),如果应用已加锁,将加锁应用的包名维护到数据库中 ②.已加锁+未加锁 == 手机中所有应用(AppInfoProvide ...
- C# 分支语句
选择语句 if,else if是如果的意思,else是另外的意思,if后面跟()括号内为判断条件,如果符合条件则进入if语句执行命令.如果不符合则不进入if语句.else后不用加条件,但是必须与if配 ...
- CSS3中的box-shadow
语法: box-shadow: h-shadow v-shadow blur spread color inset; box-shadow 向框添加一个或多个阴影.该属性是由逗号分隔的阴影列表,每个阴 ...
- java中判断字符串是否为数字的方法
一: //1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < str.length(); ...
- JSONKit解析json数据
先将第三方文件拖进工程 JSONKit.h和JSONKit.m 然后设置在ARC工程中添加MRC文件,如下图所示 #import "ViewController.h" #impor ...
- Struts2(十)OGNL标签二与Struts2标签
一.Struts2标签的优势 标签库简化了用户对标签的使用 结合OGNL使用,对于集合.对象的访问功能非常强大 提供可扩展的主题.模板支持.极大简化了视图页面的编写 不依赖任何表现层技术 Struts ...
- Effective Java 40 Design method signatures carefully
Principle Choose method names carefully. Don't go overboard in providing convenience methods. Avoid ...
- 比较全面的MySQL优化参考(上下篇)
转自:http://imysql.com/2015/05/24/mysql-optimization-reference-1.shtml 本文整理了一些MySQL的通用优化方法,做个简单的总结分享,旨 ...
- nginx中使用srcache_nginx模块构建缓存
nginx中可以将lua嵌,让nginx执行lua脚本,可以处理高并发,非阻塞的处理各种请求,openresty项目中可以使用nignx可以直接构建 srcache_nginx + redis 缓存, ...