在一个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
@Test
public 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
@Test
public 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的更多相关文章

  1. SpringXML方式配置bean的生命周期lifecycle

    在Spring中容器在初始化某个bean的时候会有相应的生命周期,类似于Servlet,有相应的init,destory等方法 例如:如下service 1 2 3 4 5 6 7 8 9 10 11 ...

  2. SpringXML方式配置bean的自动装配autowire

    Spring的自动装配,也就是定义bean的时候让spring自动帮你匹配到所需的bean,而不需要我们自己指定了. 例如: User实体类里面有一个属性role 1 2 3 4 5 6 7 publ ...

  3. 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 ...

  4. SpringXML方式配置bean的懒加载lazy-init

    lazy-init(懒加载),表示该bean在容器初始化的时候不进行初始化. 例如: <bean name="role1" class="com.fz.entity ...

  5. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  6. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  7. spring学习笔记 星球日two - 注解方式配置bean

    注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...

  8. spring学习笔记 星球日one - xml方式配置bean

    ide: idea lib包的导入:http://webcache.googleusercontent.com/search?q=cache:http://zyjustin9.iteye.com/bl ...

  9. Spring框架学习(6)使用ioc注解方式配置bean

    内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...

随机推荐

  1. 如何配置IIS服务器?

    1, 先安装IIS 然后安装vs; 注: 顺序颠倒则执行cmd命令: 1,cd \ 2,cd Windows 3, cd  Microsoft.NET 4, dir 5,cd Framework 6, ...

  2. F1Book报表在Win7下运行出现显示不完整问题

    Q: Win7环境下,明明报表要显示20多行,可是显示18行,即显示不完全情况(或常常出现报表底部内容不见了,fe:最后的签名或备注消失了)?? A:只要更新Vcf132.ocx即可.       操 ...

  3. Heartbeats

    很少有人否定,这是一首天籁.凄美动听的声音,触动的,是真正的灵魂深处.所谓“仁者见仁智者见智”,但有些东西是共通的,比如,我们的内心会被同一样东西触动.在这首动听的歌曲中,体现出那男女之间平凡又伟大的 ...

  4. 20145314郑凯杰 《Java程序设计》第7周学习总结

    20145314郑凯杰 <Java程序设计>第7周学习总结 教材学习内容总结 首先放上代码托管图片和本地代码图片: 插图4: 插图5: 插图6: 第十三章 时间与日期 13.1 认识时间与 ...

  5. /usr/bin/ld: crti.o: No such file: No such file or directory

    Problem : You are running a 64-bit linux system and trying to compile a 32-bit application and you g ...

  6. redis命令的使用

    批量删除特定前缀的keys redis-cli KEYS "prefix:*" | xargs redis-cli DEL 返回list的长度 > LPUSH test &q ...

  7. openwrt的编译系统在哪里对程序进行开机自动启动配置

    答:在include/rootfs.mk里的宏prepare_rootfs中进行的

  8. android的wifi程序随笔作业

    不用说,做前最好新建一个wifiadmin类,用来装载你所有的wifi打开关闭,wifi配置,连接情况等等wifi操作,然后main类里做一些button连接listview显示wifi网络连接等东西 ...

  9. 2017 ACM/ICPC Asia 南宁区 L The Heaviest Non-decreasing Subsequence Problem

    2017-09-24 20:15:22 writer:pprp 题目链接:https://nanti.jisuanke.com/t/17319 题意:给你一串数,给你一个处理方法,确定出这串数的权值, ...

  10. 连续取模-function

    2017-09-22 21:56:08 The shorter, the simpler. With this problem, you should be convinced of this tru ...