spring学习-ioc,(自己个人记录,边学习边记录)
1.depend on 属性:控制bean的加载顺序
当有两个bean,bean的执行顺序自上而下依次执行,例如。
<bean class="cn.tuling.beans.User" id="user" ></bean>
<bean class="cn.tuling.beans.Wife" id="wife"></bean>
按顺序先执行 id=“person” ,再执行 id=“wife” ,如果想让wife在person之间执行,1,改顺序 2,使用depends-on=“wife”
<bean class="cn.tuling.beans.User" id="user" depends-on="wife" ></bean>
<bean class="cn.tuling.beans.Wife" id="wife"></bean>-->
如果想要依赖多个bean,可以提供多个名字作为depends-on的值。以逗号、空格或者分号分割:`在这里插入代码片`
depends-on属性既可以指定初始化时间依赖性,也可以指定单独的bean,相应的销毁时间的依赖。独立定义了depends-on属性的bean会优先销毁 (相对于depends-on的bean销毁,这样depends-on可以控制销毁的顺序。
2.懒加载
bean在用的时候才加载
不会在spring容器加载的时候该bean,而是在使用时候使用,
<bean class="cn.tuling.beans.Wife" id="wife" lazy-init="true"></bean>
全部bean为懒加载,设置在beans设置default-lazy-init="true"
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
3.Bean 的作用域
singleton (默认) 每一Spring IOC容器都拥有唯一的实例对象。同一ID的bean只会创建一次,不管使用多少getbean,只会创建一次
prototype 一个Bean定义可以创建任意多个实例对象.
request 一次请求一个bean。将单个bean定义范围限定为单个HTTP请求的生命周期。 也就是说,每个HTTP请求都有自己的bean实例,它是在单个bean定义的后面创建的。 只有基于Web的Spring ApplicationContext的才可用。
session 一个客户端对应一个bean。将单个bean定义范围限定为HTTP Session的生命周期。 只有基于Web的Spring ApplicationContext的才可用。
application 一个应用只会创建一个bean。将单个bean定义范围限定为ServletContext的生命周期。 只有基于Web的Spring ApplicationContext的才可用。
websocket 一个长链接创建一个bean将单个bean定义范围限定为 WebSocket的生命周期。 只有基于Web的Spring ApplicationContext的才可用。