Spring annotation --@Scope

Qiushi Technology 2021-02-23 18:01:07
spring annotation scope


1.@Scope effect ?

@Scope Annotations are used for scope .SpringIoc The scope of a container in is as follows :
1.1 singleton( The singleton pattern , The default mode ): There is only one instance in the global
1.2 prototype( Many cases of pattern ): Every time to get bean A new instance will be generated when a new instance is created
1.3 web(request、session、globalSession)
1.3.1 request: every time HTTP Requests all generate a new instance , This instance is only in the current HTTP request Effective within
1.3.2 session: every time HTTP Requests all generate a new instance , This instance is only in the current HTTP session Effective within
1.3.3 globalSession: It's just based on portlet It will come into effect only in the middle of the year
1.4 Custom scope :

2.@Scope Usage mode ?

Directly in bean Object method @Scope Just a note
The singleton pattern is as follows

@Bean
@Scope("singleton")
public User getUser(){
System.out.println(" There is only one instance in the global !");
}

The pattern of multiple cases is as follows

@Bean
@Scope("prototype")
public User getUser(){
System.out.println(" Every time to get bean A new instance will be generated when a new instance is created ");
}

3.@Scope Use scenarios ?

spring The default type in is singleton,singleton It ensures that there is only one instance in the whole world , Improved performance , But if a non static variable is used in the instance , Can cause thread safety problems , Competition for shared resources .
When setting prototype when , Each request generates a bean example , This leads to more requests ,bean The more objects , Performance degradation , It can even lead to GC Skyrocketing .

版权声明
本文为[Qiushi Technology]所创,转载请带上原文链接,感谢
https://javamana.com/2021/02/20210223175951940t.html

  1. JAVA的函数式接口
  2. JAVA里的元祖类
  3. JQuery Gantt package to create a new ASP.NET project
  4. Binary conversion of Unicode code (Java)
  5. The functional interface of Java
  6. Yuanzu class in Java
  7. Java中的CPU占用高和内存占用高的问题排查
  8. HashMap连环问你能答出几道?
  9. IntelliJ IDEA 还能画思维导图,果然最强 IDE!
  10. java中的反射和代理
  11. Troubleshooting of high CPU and memory usage in Java
  12. How many questions can you answer?
  13. IntelliJ idea can also draw mind maps. It's really the strongest ide!
  14. Reflection and proxy in Java
  15. Java中的CPU占用高和内存占用高的问题排查
  16. Linux OOM(out of memory)
  17. mysql 自定义函数因参数名称报错
  18. Troubleshooting of high CPU and memory usage in Java
  19. Linux OOM(out of memory)
  20. MySQL user defined function error due to parameter name
  21. echarts-gl 3D 地图柱状图可视化GDP
  22. Visualization of histogram of ecarts GL 3D map
  23. 金三银四如何应对Redis面试,一文深入Redis实战实践!
  24. 阿里资深架构师定制金三银四面试整理出来的一份Java核心知识点.pdf
  25. 为什么Java开发工程师工资高,却很多人想转行?
  26. How to deal with the interview of redis!
  27. Ali senior architect customizes a Java core knowledge point sorted out in the interview of golden, silver and four.pdf
  28. Why do java development engineers have high salaries, but many people want to change careers?
  29. 用注解开发SpringMVC
  30. Developing spring MVC with annotations
  31. 编译redis报错/deps/hiredis/libhiredis.a解决
  32. Compile redis report error / DEPs / hirredis / libhirredis. A solution
  33. 用注解开发SpringMVC
  34. Developing spring MVC with annotations
  35. Spring学习笔记-01
  36. Centos安装和卸载docker
  37. Spring learning notes-01
  38. Installing and uninstalling docker in CentOS
  39. Java基础-异常
  40. Java基础-反射
  41. Java基础-继承
  42. k8s部署 (进行中)
  43. Hive-常见调优方式 && 两个面试sql
  44. 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
  45. Java basics exception
  46. Java Basics - Reflection
  47. Java Basics - inheritance
  48. K8s deployment (in progress)
  49. Hive common tuning methods & two interview SQL
  50. The loading phase of beandefinition (XML file)
  51. 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
  52. Hive-常见调优方式 && 两个面试sql
  53. The loading phase of beandefinition (XML file)
  54. Hive common tuning methods & two interview SQL
  55. iconv文件编码转换由windows文件放到linux下
  56. The code conversion of iconv file is put into Linux by windows file
  57. SpringBoot2+intellij IDEA开发前环境准备
  58. Preparation of pre development environment for springboot2 + IntelliJ idea
  59. Docker私有仓库部署
  60. Docker private warehouse deployment