简体   繁体   English

我应该在 spring 单例 bean 中完全使用静态吗

[英]Should I use static at all in spring singleton beans

As I just read, by default my created spring beans are Singletons.正如我刚刚读到的,默认情况下我创建的 spring bean 是单例。 I'm currently using the static keyword for my Loggers, reused variables, and some Lists that I want to exist only once.我目前正在将 static 关键字用于我的记录器、重用变量和一些我只想存在一次的列表。

But because alls beans are singletons I'm thinking about just removing static from everything.但是因为所有 bean 都是单例,所以我正在考虑从所有内容中删除静态。

private static final Logger LOGGER = LoggerFactory.getLogger(MatchmakingService.class);
private static final List<Lobby> QUEUE_BLOCKED = new ArrayList<>();

to

private final Logger logger = LoggerFactory.getLogger(MatchmakingService.class);
private final List<Lobby> queueBlocked = new ArrayList<>();

My question is, should I use "static" at all within the spring context?我的问题是,我应该在 spring 上下文中使用“静态”吗? If yes, why?如果是,为什么?

Spring is designed so that for the vast majority of cases you can avoid static fields. Spring 的设计使得在绝大多数情况下您可以避免使用静态字段。

Having a logger that is static final is perfectly ok.拥有一个静态最终记录器是完全可以的。 Loggers are threadsafe and are designed to be used this way.记录器是线程安全的,旨在以这种方式使用。

Having static final constants is ok.拥有静态最终常量是可以的。

Other than loggers, anything static that is not immutable should be avoided.除了记录器之外,应该避免任何不是一成不变的静态。

Also do not use instance variables for conversational state (state related to actions performed by callers of the service), because other callers can access and change that state.也不要将实例变量用于会话状态(与服务调用者执行的操作相关的状态),因为其他调用者可以访问和更改该状态。

If you are using instance or static variables to cache, get rid of them and configure a cache manager instead.如果您使用实例或静态变量来缓存,请摆脱它们并配置缓存管理器。

If your code is storing configuration data in static fields, make them instance fields and use spring to read the config data in.如果您的代码将配置数据存储在静态字段中,请将它们设为实例字段并使用 spring 读取配置数据。

Using a static logger within a class generally makes sense if it is logging to the same file or output stream (eg System.out ), regardless of which instance logs something.如果类记录到相同的文件或输出流(例如System.out ),则在类中使用静态记录器通常是有意义的,而不管哪个实例记录某些内容。 Whether the queue in your code snippit should be static or not depends on its use and cannot be answered without more details.代码片段中的队列是否应该是静态的取决于它的用途,没有更多细节就无法回答。

Good question.好问题。 In my opinion, If you business scenario stay always in same spring context, you should not use static.在我看来,如果您的业务场景始终处于相同的 spring 上下文中,则不应使用静态。 The reason is that spring bean is single in one spring context。But bean has different instance in different spring context.原因是 spring bean 在一个 spring 上下文中是单一的。但是 bean 在不同的 spring 上下文中具有不同的实例。 The below is sample code:下面是示例代码:

//  The first Spring Bean Context
ApplicationContext context1 = new FileSystemXmlApplicationContext("classpath:/ApplicationContext.xml");
Biz b1 = context1.getBean("myBean", Biz.class);

//  The second Spring Bean Context
ApplicationContext context2 = new FileSystemXmlApplicationContext("classpath:/ApplicationContext.xml");
Biz b2 = context2.getBean("myBean", Biz.class);

//  The result is false, because of creating two instances of Biz
System.out.println(b1 == b2);

But static variable has one instance in one JVM.但是静态变量在一个 JVM 中有一个实例。 So for performance and robust,you should use static more often.所以为了性能和健壮性,你应该更频繁地使用静态。 In another word, you can not make all class as bean in a program.换句话说,你不能把一个程序中的所有类都做成bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM