• 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”

    结果:

      

              

  • 相关阅读:
    前端的貌似不顺道整个 小程序,不是那么回事哈
    sql server 大批数据插入时,时间过长的问题
    web api 权限控制
    推荐一个测试Web API, web service工具
    ConcurrentDictionary的用法
    Socket
    微信小程序(一)
    List常用几种方式
    c# 文件下载
    自定义身份验证
  • 原文地址:https://www.cnblogs.com/wwww2/p/12594487.html
Copyright © 2020-2023  润新知