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

  • 相关阅读:
    管理上第一是用人
    意义只存在于关系中,由其定义
    苦与累在希望面前啥也不是
    人是一切,组织是一切
    UI设计
    以理服人需要什么?
    灵活性是原则性基础上的灵活
    软件行业深层的文化属性
    自然原始分工
    把一个系统维护好需要做哪些工作?
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9104715.html
Copyright © 2011-2022 走看看