答:

@Component注解默认实例化的对象是单例,如果想声明成多例对象可以使用@Scope("prototype")

@Repository默认单例

@Service默认单例

@Controller默认多例

 

---------------------------------------------------------------

 

测试1:

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

 

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

 

2.开启@Component 多例。

 

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

 

 

 

 

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

 

 3.留意点,针对单例的,仅仅是在spring容器里单例,但是这个类依然可以new ,这个时候 new 出来的又不是单例了,所以最好全部交给spring来管理把,用到的地方就@autowire 注入。

 

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

 

 

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

4.@Configuration 可以达到同样的效果,也是单例,用@component 类似于分散式(用起来比较方便),@Configuration是集中式 bean管理,用起来不方便废弃。

 

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

 

 

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

 

 

 

 

@Component、@Repository、@Service、@Controller默认是单例还是多例?

 

 

相关文章:

  • 2021-10-03
  • 2021-10-01
  • 2022-12-23
  • 2021-08-23
  • 2021-06-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-05-14
相关资源
相似解决方案