spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session

在配置spring mvc的action时,可以对action使用 prototype

1    <!-- singleton: 默认单例 prototype: 多例 request ,session和global session: 只适用于web程序 -->
2     <bean id="scope_prototype" class="com.lee.spring005.scope.Scope"
3         scope="prototype"></bean>
4
5     <!-- destroy-method 一般在数据源的时候用到,关闭容易后就销毁连接 -->
6     <bean id="initDestory" class="com.lee.spring006.init_destory.InitDestory"
7         init-method="init" destroy-method="destory"></bean>

bean的创建销毁过程:

 1 package com.lee.spring006.init_destory;
 2
 3 public class InitDestory {
 4
 5     public InitDestory() {
 6         System.out.println("InitDestory() ");
 7     }
 8
 9     public void hello() {
10         System.out.println("Hello InitDestory!");
11     }
12
13     public void init() {
14         System.out.println("init");
15     }
16
17     public void destory() {
18         System.out.println("destory");
19     }
20 }

测试:

 1     @Test
 2     public void testInitDestory() {
 3
 4         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 5
 6         InitDestory initDestory = (InitDestory)context.getBean("initDestory");
 7         initDestory.hello();
 8
 9         ClassPathXmlApplicationContext app = (ClassPathXmlApplicationContext)context;
10         app.close();
11     }

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

转载于:https://www.cnblogs.com/leechenxiang/p/5305377.html

Spring 一二事(4) - 单例相关推荐

  1. Spring(07)——单例注入多例之lookup-method

    2019独角兽企业重金招聘Python工程师标准>>> Spring(07)--单例注入多例之lookup-method 博客分类: spring 7 单例注入多例之lookup-m ...

  2. spring框架中的单例Beans是线程安全的么?

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

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

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

  4. Spring中bean的单例与多例

    Spring中bean的单例与多例 前言 Spring中单例与多例 如何配置单例/多例 单例/多例Bean的使用事项 总结 前言 之前其实已经学习过对于单例模式的使用单例模式讲解,也用过一段时间的Sp ...

  5. 结合Spring源码学习单例设计模式

    之前我学习了 Spring Ioc,明白了 Spring IoC 容器是一个管理Bean的容器,在Spring的定义中,它要求所有的IoC容器都需要实现接口 BeanFactory ,它是一个顶级容器 ...

  6. Spring的Controller是单例还是多例?怎么保证并发的安全

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:riemann_ blog.csdn.net/riemann ...

  7. Spring 的 Controller 是单例还是多例?怎么保证并发的安全

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | riemann_ 来源 | blog.csdn ...

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

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

  9. 面试官:Spring创建好的单例对象存在线程安全问题吗?

    点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/jdk_wangtaida/ article/details/88738228 前言 这是我在一次面试中被问到过的问题,但是当时 ...

最新文章

  1. C#中Timer组件用法
  2. 中小企业CRM评测-客户服务_企能
  3. django和flask用MD5加密密码
  4. 404 错误页面_如何设计404错误页面,以使用户留在您的网站上
  5. Mr.J-- jQuery学习笔记(二十九)--属性操作方法(获取属性判断)
  6. Linux CentOS修改网卡IP/网关设置
  7. 神经网络之智能科学与技术专业
  8. 微信 8.0 「裂开」「炸弹」的特效代码
  9. python命名空间与作用域
  10. 工业大数据:中国智造下的“数字新基建”
  11. 2020Android面试题跳楼大整理,京东-字节跳动面经+个人总结+心得
  12. 鹏业BIM三维安装算量软件一次购买还是收年费
  13. 2021-04-28-飞机大战-001-day5-游戏滚动背景
  14. 模式分解的无损连接性之深入剖析
  15. python后端开发的缺点_前后端分离优缺点
  16. 防火墙添加ip白名单_宝塔防火墙IP白名单添加/导入云盾YUNDUN高防CDN节点IP段
  17. 《电子设计工程》期刊简介
  18. linux如何让命令是中文,linux centos 让man命令帮助显示简体中文
  19. 实验四 MIPS寄存器文件设计 Logisim
  20. 中国畜牧养殖机械设备行业研究及未来趋势报告(2022版)

热门文章

  1. eplan图纸怎么发给别人_EPLAN标签导出材料清单(附模板+图文教程)
  2. python爬取网页公开数据_如何用Python爬取网页数据
  3. 【AI不惑境】网络深度对深度学习模型性能有什么影响?
  4. 【AI-1000问】训练为什么要分测试集和验证集?
  5. 微机原理及接口技术-6
  6. c语言多维数组指针地址讲解,C语言入门之多维数组的指针变量
  7. Abstract Factory抽象工厂模式
  8. 二.安全NA之ASA基础
  9. webapi同时支持post和get报404错误
  10. 【转】Spring 的下载、安装和使用