zoukankan      html  css  js  c++  java
  • 二、配置你的bean

    一、bean有五种作用域

    <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对应一个实例


    在不配置scope的情况下,默认scope="singleton"

    当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实现中文转拼音
    JS中的call、apply、bind方法
    python 过滤html方法
    css 多出一行或多行后显示...的方法
    js 中文排序
    eclipse小技巧
    npm安装及webpack打包小demo
    zan扩展安装
    vagrant安装centos7
    centos7 nginx访问目录403解决
  • 原文地址:https://www.cnblogs.com/myz666/p/8098211.html
Copyright © 2011-2022 走看看