0
点赞
收藏
分享

微信扫一扫

spring bean的使用范围:singleton,prototype,request,session,application

北溟有渔夫 2022-01-22 阅读 17

singleton prototype bean的学习
/**

  • 结论一:singleton bean 只有一个;prototype bean每次都会创建新的
  • 结论二:对于集合对象,会有两个user对象,一个是singleton bean ,一个是 prototype bean
  • 结论三:singleton和prototype的生命周期
  •   两类bean均会执行初始化方法回调,但只有singleton会执行调回回调
    
  •   那么如何销毁prototype bean呢? 通过继承DisposableBean接口,实现destroy方法,手动销毁
    

*/

知识点:
transient关键之:表示反序列化,不需要被序列化存储,反序列化时根本没有从文件中获取到信息
BeanNameAware 接口的 setBeanName 方法
@PostConstruct和@PreDestroy 注解
map类中entry遍历Map.Entry<String,User> entry: this.mUsers.entrySet()
ConfigurableListableBeanFactory 中的getBeanDefinition
@Scope
@Autowired
@Bean
@Qualified

web中request的作用域
requestScope中的user bean 为CGLIB的代理对象
返回值页面的对象被转为scopedTarget.user,是一个新的对象
每个请求都是一个新的对象

SessionScope范围是session,在浏览器不关闭的情况下返回至页面的scopedTarget.user 为同一个对象

举报

相关推荐

Spring中Bean的作用范围

0 条评论