zoukankan      html  css  js  c++  java
  • 三、bean的作用域

    一、bean有五种作用域,在不配置scope的情况下,默认scope="singleton"

    <bean scope="singleton" >
    在每个spring IOC容器中一个bean仅有一个实例
    <bean scope="prototype" >
    一个bean定义对应多个实例
    <bean scope="request" >
    在一次http请求中,一个bean对应一个实例
    <bean scope="session" >
    在一个httpsession中一个bean对应一个实例
    <bean scope="global-session" >
    在一个全局httpsession中,一个bean对应一个实例

    当applicationContext.xml是

    <bean id="user" class="com.beans.User">
            <property name="name" value="蔡文姬"/>
        </bean>

    或者

    <bean id="user" class="com.beans.User" scope="singleton">
            <property name="name" value="蔡文姬"/>
        </bean>

    运行下列代码

    package com.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.beans.User;
    
    public class Test {
        
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            User user1=(User) ac.getBean("user");
            User user2=(User) ac.getBean("user");
            System.out.println(user1+" "+user2);
        }
    }

    打出com.beans.User@1af33d6 com.beans.User@1af33d6,说明两个user是同一个对象

    同理测试scope="prototype"得到两个user的地址不同,说明两个user是两个不同的对象

     

  • 相关阅读:
    JS基础_函数的简介
    frp 使用入门
    树莓派开启smb
    python 反射调用
    VIDEOIO ERROR: V4L: can't open camera by index 0 for raspberryPi
    face_recognition 人脸识别报错
    安装FFMpeg CentOS 7
    Centos 7 smb 安装使用
    ImportError: libQtTest.so.4: cannot open shared
    Raspberry Pi 3b+ 配置摄像头
  • 原文地址:https://www.cnblogs.com/myz666/p/8186417.html
Copyright © 2011-2022 走看看