Spring_2_Spring中lazy-init和scope属性
2017-07-09 10:25
483 查看
1)springTest类:
2)Bean的XML配置文件:
public class springTest { @Test public void instanceSpring() { AbstractApplicationContext ctx = new <span style="white-space:pre"> </span>ClassPathXmlApplicationContext("springXml/beans.xml"); <span style="white-space:pre"> </span>// 该实例输出结果为true:说明Spring容器中一个bean定义仅仅有一个对象实例 // 当bean的配置文件里设置scope="prototype"时,输出为false // 说明:每次生成一个新的bean对象 <span style="white-space:pre"> </span>PersonService personService1 = (PersonService)ctx.getBean("personservice1"); PersonService personService2 = (PersonService)ctx.getBean("personservice1"); <span style="white-space:pre"> </span>System.out.println(personService1 == personService2); } }
2)Bean的XML配置文件:
<span style="font-size:14px;"><strong><?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-2.5.xsd"> <!-- 使用类构造器实例化Bean 推荐使用 --> <!-- Spring容器中一个bean定义仅仅有一个对象实例, 默认情况会在容器启动时初始化bean, 但能够在 Bean节点中指定lazy-init="ture", 这时仅仅有第一次获取bean才会初始化bean. 假设想对全部的bean都实行延迟初始化, 能够在根节点beans设置default-lazy-init="true". 假设设置scope="prototype",则每次都会生成一个新的bean对象. 假设设置init-method="methodName",实例化对象时会调用该方法. --> <bean id="personService1" class="springDao.PersonServiceBean" scope="prototype" init-method="init" ></bean> </beans></strong></span>
相关文章推荐
- Spring_2_Spring中lazy-init和scope属性
- spring(4)_ bean属性 scope:作用域和lazy-init
- 容器对象spring(4)_ bean属性 scope:作用域和lazy-init
- spring beans标签的default-lazy-init属性
- Spring入门之创建Bean的时间以及lazy-init属性
- spring两个重要属性lazy-init和abstract
- spring技术内幕笔记:lazy-init属性和预实例化
- Spring - bean的lazy-init属性(懒加载)
- spring两个重要属性lazy-init和abstract
- Spring源码分析之lazy-init属性的配置
- spring两个重要属性lazy-init和abstract
- Spring的lazy-init属性延迟初始化Bean
- spring 中属性scope 的prototype(有状态)和singleton(无状态)
- SPRING中属性SCOPE的prototype是什么意思
- spring中lazy-init详解
- Spring表单的initBinder:绑定表单复杂属性
- spring启动的优化 default-lazy-init=true
- SPRING中属性SCOPE的prototype是什么意思
- Spring启动太慢?&Spring 配置中的 default-lazy-init="false"
- Spring的bean标签的scope和parent属性