(转)配置Spring管理的bean的作用域
http://blog.csdn.net/yerenyuan_pku/article/details/52833477
Spring管理的bean的作用域有:
singleton
在每个Spring IoC容器中,一个bean定义只有一个对象实例。
以Spring的三种实例化Bean的方式的案例为基础,我们举例说明。首先我们将Spring的配置文件——beans.xml的内容改为:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean> </beans>然后再将SpringTest类的代码改为:
public class SpringTest { @Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
PersonService personService1 = (PersonService) ctx.getBean("personService"); // 从Spring容器取得bean
PersonService personService2 = (PersonService) ctx.getBean("personService");
System.out.println(personService1 == personService2); // 输出true,两个变量所引用的对象是同一个,证实了默认情况下这个bean交给Spring容器管理之后,这个bean是一个单实例。
} }最后,测试test()方法,Eclipse的控制台输出true,说明了默认情况下bean交给Spring容器管理之后,这个bean就是一个单实例(单例模式)的,即每次调用getBean()方法,获取到的都是同一个bean实例。
prototype
现在我们有了一个新的需求,那就是在客户端每次调用getBean()方法,获取到的都是一个新的bean实例,这个时候就需要用到prototype作用域了。每次从容器获取bean都是新的对象。
为了证明这一点,我们只须将Spring的配置文件——beans.xml的内容改为:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"></bean> </beans>SpringTest类的代码不用改动,此时测试test()方法,Eclipse的控制台会输出false。这就已经证实了若bean的作用域置为prototype,那么每次从Spring容器获取bean都将是新的对象。
- request
- session
- global session
(转)配置Spring管理的bean的作用域的更多相关文章
- Spring第五弹—–配置Spring管理的bean的作用域和生命周期
singleton (默认方式) 在每个Spring IoC容器中一个bean定义只有一个对象实例.默认情况下会在容器启动时初始化bean,但我们可以指定bean节点的lazy-init=“true” ...
- 配置spring管理的bean的作用域
.singleton 在每一个spring Ioc容器中一个bean定义只有一个对象实例.默认情况下会在容器启动时初始化bean,但我们可以指定bean节点的lazy-init = "tru ...
- (转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011 bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们 ...
- 怎么随时获取Spring的上下文ApplicaitonContext,和Spring管理的Bean
BeanFactory接口 Interface BeanFactory getBean <T> T getBean(String name, Class<T> required ...
- Spring基础11——Bean的作用域
1.Bean的作用域种类 Spring中的bean的作用域分为四种:singleton.prototype.session.request,后两种很少使用,下面我们主要来学习前两种 2.singlet ...
- 将spring管理的bean使用注解的方式注入到servlet中
Filter和Servlet中不能直接注解使用spring的bean,因为这两个都是servlet容器维护管理的,当然也有实现方法,如下: 1.创建一个AbstractServlet 抽象类,让你的所 ...
- 为何在新线程中使用注解获取不到Spring管理的Bean
新建的线程类NewThread,在这个类中国使用Spring的注解获取Service,为null 网上已有这种问题的解决方案,但是为何在新线程中使用注解获取不到Spring管理的Bean? 问了老大, ...
- 手动获取被spring管理的bean对象工具
在netty handler开发中,我们无法将spring的依赖注入到Handler中,无法进行数据库的操作,这时候我们就需要手动获取被spring管理的bean对象: 创建一个 imp ...
- Spring 学习笔记 Bean的作用域
在配置文件中定义Bean时,用户不但可以配置Bean的属性值以及相互之间的依赖关系,还可以定义Bean的作用域.作用域将对Bean的生命周期和创建方式产生影响.在低版本的Spring中,仅有两个作用域 ...
随机推荐
- @font-face 用fontsquirrel把ttf文件获取别的文件格式
@font-face是css3的一个模块,但是@font-face这个功能早在IE4就支持了,他主要是把自己定义的Web字体嵌入到你的网页中, @font-face { font-family: &l ...
- 020--python函数基础知识考试(包括:函数_递归等知识)
1.列举布尔值为 False 的值 空,None,0, False, '', [], {}, () 2.写函数: 根据范围获取其中 3 和 7 整除的所有数的和,并返回调用者:符合条件的数字个数以及符 ...
- 任务29:自己动手构建RequestDelegate管道
cmd创建一个控制台应用程序 dotnet new console --name MyPipeline 用VSCode打开这个项目 新建类RequestDelegate.cs的类文件复制Program ...
- 任务36:应用Jwtbearer Authentication
任务36:应用Jwtbearer Authentication D:\MyDemos\jesse 新建项目:dotnet new webapi --name JwtAuthSample VS2017运 ...
- ccflow_004请假流程-傻瓜表单-经典模式
ccflow_004请假流程-傻瓜表单-经典模式
- emacs配置文件的基础知识 (转载)
转自:http://blog.csdn.net/schumyxp/article/details/2278268 emacs的配置文件,叫作.emacs,是个隐藏文件,存在于当前用户的根目录下面,也就 ...
- cocos2d-html5基础
1 环境搭建 版本Cocos2d-html5-v2.2,tomcat7.0 配置tomcat,然后直接解压Cocos2d-html5-v2.2.zip,访问解压后的根目录的index.html就可以看 ...
- 笔记-JavaWeb学习之旅2
数据库的基本概念 1.数据库:DataBase 简称 DB,用于存储和管理数据的仓库 特点: 1.持久化存储数据的,其实数据库就是一个文件系统, 2.方便存储和管理数据 3.使用了统一操作数据库 -- ...
- js锚点
锚点,在地址栏http://localhost:8000/Demo1/js锚点.html#a 跳转到指定位置可以是 <a>标签的name 也可以是标签的id. 曾经看到有锚点的页面,但 ...
- 「软件」仿站小工具v9.0
仿站小工具是通过网址下载静态网页的工具.从输入的网址下载html代码,提取出JS.Css.Image.Picture.Flash等静态文件网址,再从下载完好的Css代码中提取出Image静态文件网址, ...