码迷,mamicode.com
首页 > 编程语言 > 详细

Spring 单例模式和多例模式

时间:2019-02-28 10:27:02      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:bean   getbean   autowire   cat   ica   red   其他   需要   wired   

1.Spring中的对象默认都是 单例模式。

2.使用 @Scope("prototype") 注解来使对象成为多例模式。

3.通过@Autowired 注入的Service 或者是其他实例其实是单例的。

4.通过 ApplicationContext.getBean(C.class); 获取的实例是多例的。

 

 

 

总结:在存在并发的时候,每个需要被注入的类、对象 都使用@Scope("prototype") 注解成为多例,

   每个需要被获取的对象通过ApplicationContext.getBean(C.class);来获取,确保每个线程获取的对象都是新的。

Spring 单例模式和多例模式

标签:bean   getbean   autowire   cat   ica   red   其他   需要   wired   

原文地址:https://www.cnblogs.com/getchen/p/10448538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!