SpringXML方式配置bean的生存范围Scope
在一个bean的配置里面可以指定一个属性Scope,也就是bean的范围,bean的生命周期。
Scope可取的值5种:singleton(默认)、prototype、request、session、global session
其中最常用的就是:singleton和prototype,其他的三个是和web相关的,很少使用。
singleton:也就是单例模式。表示这个bean是单例模式,每次获取都是同一个bean
prototype:多例模式,也就是每次获取的都是一个新对象,使用场景:在action上需要设置为prototype
例如:user这个bean,默认的Scope属性我们没有配置,也就是singleton模式
|
1
2
3
4
5
6
|
<bean name="user" class="com.fz.entity.User" > <property name="id" value="1"></property> <property name="username" value="fangzheng"></property> <property name="password" value="123456"></property> <property name="role" ref="role"></property></bean> |
测试singleton,结果为true
|
1
2
3
4
5
6
7
|
@Testpublic void getProperties(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); User user1 = (User) ctx.getBean("user"); User user2 = (User) ctx.getBean("user"); System.out.println(user1 == user2);//结果为true } |
添加scope=prototype
在<bean>上加入scope=prototype之后。
|
1
2
3
4
5
6
|
<bean name="user" class="com.fz.entity.User" scope="prototype"> <property name="id" value="1"></property> <property name="username" value="fangzheng"></property> <property name="password" value="123456"></property> <property name="role" ref="role"></property></bean> |
测试prototype,结果为false
|
1
2
3
4
5
6
7
|
@Testpublic void getProperties(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); User user1 = (User) ctx.getBean("user"); User user2 = (User) ctx.getBean("user"); System.out.println(user1 == user2);//结果为false} |
SpringXML方式配置bean的生存范围Scope的更多相关文章
- SpringXML方式配置bean的生命周期lifecycle
在Spring中容器在初始化某个bean的时候会有相应的生命周期,类似于Servlet,有相应的init,destory等方法 例如:如下service 1 2 3 4 5 6 7 8 9 10 11 ...
- SpringXML方式配置bean的自动装配autowire
Spring的自动装配,也就是定义bean的时候让spring自动帮你匹配到所需的bean,而不需要我们自己指定了. 例如: User实体类里面有一个属性role 1 2 3 4 5 6 7 publ ...
- SpringXML方式配置bean的集合注入:list,map,properties
新建一个bean,设置相应的集合属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Collecti ...
- SpringXML方式配置bean的懒加载lazy-init
lazy-init(懒加载),表示该bean在容器初始化的时候不进行初始化. 例如: <bean name="role1" class="com.fz.entity ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
- spring学习笔记 星球日one - xml方式配置bean
ide: idea lib包的导入:http://webcache.googleusercontent.com/search?q=cache:http://zyjustin9.iteye.com/bl ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
随机推荐
- centos上安装redmine
1.下载bitnami的redmine安装包 https://bitnami.com/stack/redmine/installer 2.安装remine ./bitnami-redmine-3.3. ...
- 20145316 《Java程序设计》第1周学习总结
20145316 <Java程序设计>第1周学习总结 教材学习内容总结 一.了解java语言: 1.Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言. Java 技术具有卓越的 ...
- C#中 哪些是值类型 哪些是引用类型
DateTime属于 结构类型,所以是 值类型 在 C#中 简单类型,结构类型,枚举类型是值类型:其余的:接口,类,字符串,数组,委托都是引用类型
- 关于MVC 中EF调用存储过程
Entity Framework 4.3 中使用存储过程 分类:ASP.NET MVC 3, ASP.NET 0 尽管 Entit ...
- git推送到github报错:error: The requested URL returned error: 403 Forbidden while accessing https://github.com
最近使用git命令从github克隆仓库到版本,然后进行提交到github时报错如下: [root@node1 git_test]# git push origin mastererror: The ...
- HelloWorld程序编写调试及错误解决
HelloWorld程序编写调试及错误解决 eclipse软件编写 相较于windows内置记事本,eclipse编写程序更为简单快捷.由其生成的程序模板编写如下: package helloworl ...
- 访问ashx一般应用程序
浏览器中的地址栏键入要访问页面的地址:回车(是和服务器软件打交道)----向服务器发送请求(以http协议为基础,服务器按照此协议解释理解接收到的数据),服务器接收到发送的请求,根据请求信息知道当前所 ...
- bat(续七)-for语句(循环结构)
for语句(循环结构) for语句可以实现类似于C语言里面的循环结构,当然for语句的功能要更强大一点,通过不同的开关可以实现更多的功能.for语句有多个开关,不同开关将会实现不同的功能. 1.无 ...
- An Overview of Forms Authentication (C#)
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-security/introduction/an-o ...
- CTR的贝叶斯平滑
参考论文: Click-Through Rate Estimation for Rare Events in Online Advertising 参考的博客: 1.https://jiayi797. ...