zoukankan      html  css  js  c++  java
  • 【Spring】学习笔记004Bean的作用域

      Spring官方给出的bean作用域如下

    1. 单例模式

      单例模式是Spring的默认机制,每次从容器中get的时候,都会创建一样的对象

    <bean id="helloBean" class="com.aircl.domain.Hello" scope="singleton">
        <property name="str" value="Hello Spring"></property>
    </bean>

      测试

        @Test
        public void testHello(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            Hello helloBean = (Hello) context.getBean("helloBean");
            Hello helloBean1 = (Hello) context.getBean("helloBean");
            System.out.println(helloBean == helloBean1);
        }

    2. 原型模式

      原型模式下,每次从容器中get的时候,都会产生一个新对象

    <bean id="helloBean" class="com.aircl.domain.Hello" scope="prototype">
        <property name="str" value="Hello Spring"></property>
    </bean>

      测试

      如上相同代码

    3. 其他模式

      其余模式在Spring MVC下生效,在此暂不讨论。

  • 相关阅读:
    用表组织数据
    SQL Server 2008创建数据库
    c#字符串常用方法
    属性升级介绍
    c#语法
    初识C#
    CSS动画
    YCSB性能测试工具使用
    高性能的Redis代理TwemProxy
    JVM垃圾回收总结
  • 原文地址:https://www.cnblogs.com/AirCL/p/14372072.html
Copyright © 2011-2022 走看看