课时13:作用域

.1)bean的作用域

   1.单例对象 mybatis默认就单例 singleton 结果返回true 说明引用地址是一样的

<!--   注入dao层-->
    <bean id="iStudentDaoImpl" class="org.bd.dao.impl.IStudentDaoImpl"></bean>
    public static void test1(){
        ApplicationContext context=new FileSystemXmlApplicationContext("E:\workJavaEE\Spring\work\SpringAnoo\src\resulrces\applicationContextt.xml");
        IStudentDao iStudentDao1=(IStudentDao)context.getBean("iStudentDaoImpl");
        IStudentDao iStudentDao2=(IStudentDao)context.getBean("iStudentDaoImpl");
        System.out.println(iStudentDao1==iStudentDao2);
    }

  2.多例对象 prototype 结果返回了false 因为不是一个引用地址不一样了 多实例了

<!--   注入dao层-->    <bean id="iStudentDaoImpl" class="org.bd.dao.impl.IStudentDaoImpl" scope="prototype"></bean>

  3.两种执行的时机

    3.1 singleton :容器在初始化的时候,就会创建对象;以后在getBean时,不在创建对象

      3.1.1 单例也支持延迟加载bean 也就是使用的时候才创建 lazy-init="true"

<!--   注入dao层-->   
<bean id="iStudentDaoImpl" class="org.bd.dao.impl.IStudentDaoImpl" lazy-init="true">
    </bean>

    3.2 prototype :容器在初始化的时候,不创建对象;只是在每次使用的时候(每次从容器取出对象的时候,context.getBean(xxx)),在创建对象;并且 每次getBean()都会创建新的对象

    

原文地址:https://www.cnblogs.com/thisHBZ/p/12512123.html