zoukankan      html  css  js  c++  java
  • Bean的作用域

    1,Bean对象

    package com.songyan.scope;
    
    public class Bean4 {
    
    }

    2,配置文件

    <?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="bean4" class="com.songyan.scope.Bean4" scope="singleton"></bean>
    </beans>

    3,测试类

    package com.songyan.scope;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class ScopeTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/scope/Beans4.xml");
        Bean4 bean4=(Bean4)applicationContext.getBean("bean4");
        System.out.println(bean4);
        bean4=(Bean4)applicationContext.getBean("bean4");
        System.out.println(bean4);
    }
    }

    4,结果

    5,当作用域改为"prototype"

    <?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="bean4" class="com.songyan.scope.Bean4" scope="prototype"></bean>
    </beans>

    6,输出结果

  • 相关阅读:
    解题报告 校门外的树
    解题报告 最长上升子序列
    解题报告 poj 2528 (罕见的浮水法解这个题的。。。。。。)
    解题报告 整数划分
    悲剧的程序员
    解题报告 sgu 102
    NOI 2007 社交网络
    解题报告 poj 1087
    知识点梳理 离散化
    解题报告 noi 2002 robot
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9104715.html
Copyright © 2011-2022 走看看