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

  • 相关阅读:
    VMware Workstation的三种网络连接方式
    sql:unix下的sql操作
    linux脚本: makefile以及链接库
    unix shell: ksh fundamental(Korn Shell)
    linux c: core dump
    linux命令:scp
    Eclipse更改默认工作目录的方法
    linux: 可重入函数与不可重入函数
    linux环境 :Linux 共享库LIBRARY_PATH, LD_LIBRARY_PATH 与ld.so.conf
    linux命令:Linux命令大全
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9104715.html
Copyright © 2011-2022 走看看