zoukankan      html  css  js  c++  java
  • spring--Scope

     spring  之 Scope

    Scope有两个值:singleton(默认)  prototype

                                 singleton是单例的

                                 prototype是非单例的

       

    如:

       Person.java


    package
    com; public class Person { }
    //applicationContext.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="p" class="com.Person" scope="singleton"></bean> --> <bean id="p" class="com.Person" scope="prototype"></bean> </beans>

    测试:

    package com;
    
    import org.junit.Test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.factory.BeanFactory;
    
    public class TestDemo {
    
        @Test
        public void test01() {
            //启动spring容器,读取src下applicationContext.xml文件
           ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
           //调用spring容器创建对象
           Person p1 = (Person) ac.getBean("p");
           Person p2 = (Person) ac.getBean("p");
           Person p3 = (Person) ac.getBean("p");
           System.out.println(p1);
           System.out.println(p2);
           System.out.println(p3);
        }
        
        
    }

    如果scope=“prototype”

    结果:

     

    如果scope=“singleton”

    结果:

      

              

  • 相关阅读:
    1051 Wooden Sticks(贪心-3)
    97 等价交换(贪心-2)
    python文件操作
    python学习-day 2
    python学习-day 1
    Python 测试题目-1
    Python list和dict方法
    Python 字符串
    while循环语句
    Python if判断语句
  • 原文地址:https://www.cnblogs.com/wwww2/p/12594487.html
Copyright © 2011-2022 走看看