spring管理的bean的生命周期

注解配置中给对应的bean添加org.springframework.context.annotation.Scope注解

//@Scope("singleton") // 容器范围
//@Scope("prototype") //每次获取bean都new
//@Scope("request") //一次http请求一个实例
//@Scope("session") //一个http session中
//@Scope("globalSession")// 一个全局session中有效
//@Scope("application") //application作用域中

//一下作用域需要配置web并在web.xml中配置org.springframework.web.context.request.RequestContextListener监听

不知道上面的监听是否是必须的,不过在spring mvc项目中可以不用加上面的监听也可以设置bean的生命周期


测试,可以直接打印出对应bean的对象地址查看是否一样

/**
*
* Specifies the scope to use for the annotated component/bean.
* @see ConfigurableBeanFactory#SCOPE_SINGLETON
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
* @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST
* @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION
* org.springframework.web.context.WebApplicationContext
*/

猜你喜欢

转载自jie66989.iteye.com/blog/1708840