java - spring中配置的bean是单例么?如何证明
PHPz
PHPz 2017-04-18 09:42:21
0
4
570

比如,在spring的配置文件中,配置了如下的bean。

<bean id = "privilegeRoleBo" 
    class = "com.alibaba.tboss.biz.privilege.impl.PrivilegeRoleBoImpl"></bean>

上面的bean应该不是单例的,因为如果项目部署在集群上,集群的每台机器上都维护着一个名字为privilegeRoleBo的bean吧,这样的话,privilegeRoleBo这个bean就不是单例的吧?如果我要配置一个单例的bean,该如何配置呢?
另外,如何写一个小Demo,可以验证一下当前的bean是否是单例??

PHPz
PHPz

学习是最好的投资!

reply all(4)
Ty80

The answer above is correct. Java singletons must be limited to one JVM, just like Servlet 只会存在一个实例This sentence is also limited to one JVM. If there are multiple JVMs, there will be one instance for each JVM. In fact, for many cases, there is no big problem that instances cannot be migrated between multiple JVMs, such as database connections or read-only configuration file information classes. But if you want to synchronize multiple JVMs, you must use other methods, such as file locks, distribution coordination services, etc. For example, the HttpSession object is synchronized using JVM migration.

巴扎黑

The singleton at the Java language level refers to the same JVM, and the cluster cannot be used. If the singleton you want to set up is for data sharing, then the data should be stored in a database or redis or something like that

刘奇

The problem of cluster singleton has been mentioned above. Let me talk about how to verify whether the current bean is a singleton. You can run the project in debug mode, send two requests, and check the address of the bean used. privilegeRoleBo They are not the same. If they are the same, they are singletons

刘奇

bean has a property scope

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template