• 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,输出结果

  • 相关阅读:
    vs2010 在函数级别设置优化
    从快速排序开始的代码演化
    pocket API学习笔记
    工作笔记:复制文件--从windows到ubuntu,再到fedora
    Win10 64位 Apache 2.4+MySQL 5.7+PHP 7环境搭建
    Json简介
    TCP/IP 协议详解和运作过程
    PHP 编程中 10 个最常见的错误,你犯过几个?
    php中的扩展解析
    php.ini设置详解
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9104715.html
Copyright © 2020-2023  润新知