简体   繁体   English

静态变量与单例

[英]Static variable vs singleton

I'm making Java REST application. 我正在制作Java REST应用程序。 I wonder how should I implement my services - should I use static service variables for whole application or make services as singletons like it was made in Spring MVC. 我想知道我应该如何实现我的服务-我应该为整个应用程序使用静态服务变量还是像在Spring MVC中那样使服务成为单例。 Is there any difference between singleton object and initializing object only once during application? 在应用期间,单例对象和仅初始化对象之间有什么区别?

如果您想拥有一些实用的方法或常量变量,则应选择使用Static;而当类可以具有状态并且状态可以更改(但对象仍然是一个)时,Singelton就会出现。

Create services as singleton that way you get more flexibility at runtime because you can inject any implementation of your services without changing the code. 将服务创建为单例,这样可以在运行时获得更大的灵活性,因为您可以注入服务的任何实现而无需更改代码。 If your idea is to share some variables using service class then mark those as final variables. 如果您的想法是使用服务类共享某些变量,则将其标记为最终变量。

Basically the fact that we make objects singleton in our systems is that we need to make sure no other objects will be created but the one we create. 从根本上说,我们使对象在系统中成为单例的事实是,我们需要确保除了创建的对象之外,不会创建其他任何对象。 There may be a lot of reasons to make singleton objects as security vulnerabilities, resource hanging problems and so on. 将单例对象作为安全漏洞,资源挂起问题等可能有很多原因。 But still there is no such a way that this is only the correct way to do this thing like that. 但是,仍然没有这样的方法,这仅仅是做这种事情的正确方法。 But using Spring MVC has its own advantages, we no need to manage the instance as its already handled by spring. 但是使用Spring MVC有其自身的优势,我们不需要管理实例,因为它已经由spring处理。 For example, when it comes to services, you may require some configurations to be loaded to the service in order to function. 例如,涉及服务时,您可能需要将一些配置加载到服务中才能运行。 By using Spring MVC beans, there is a functionality to change your configurations in run-time without re-deploying. 通过使用Spring MVC bean,可以在运行时更改您的配置而无需重新部署。

@Service
@RefreshScope
@EnableConfigurationProperties(UserManagementConfig.class)
public class UserManagementService
{

this UserManagementService is a spring service, which will be a singleton bean inside the spring context, and its configurations are loaded through UserManagementConfig class. 此UserManagementService是spring服务,它将是spring上下文内的单例bean,其配置通过UserManagementConfig类加载。 In this way you have the advantage that I mentioned above. 这样,您便拥有了我上面提到的优势。 so if you create static instances, you cant do this. 因此,如果您创建静态实例,则无法执行此操作。 So my recommendation is better use Spring MVC. 所以我的建议是最好使用Spring MVC。

should I use static service variables for whole application or make services as singletons 我应该为整个应用程序使用静态服务变量还是将服务作为单例

It depends. 这取决于。 You have to ask yourself two questions to come up with an answer: 您必须问自己两个问题才能得出答案:

Where is that static variable stored? static variable存储在哪里?

You have 2 Options: 您有2个选项:

  1. Declare a final class Services which holds all available services as public static final variables. 声明一个final class Services ,它将所有可用服务作为public static final变量保存。
  2. Create in every service class a public static final variable, called INSTANCE 在每个服务类中创建一个public static final变量,称为INSTANCE

You see that the first point will have all classes in the same place. 您会看到第一点将所有类都放在同一位置。 Could possibly get clustered, unreadable and not very easy to maintain. 可能会变得集群化,难以理解并且很难维护。

For the second point you're almost approaching the singleton case. 第二点,您几乎接近单例情况。

Do I need to initialize a service lazily or eagerly? 我需要懒惰还是急于初始化服务?

You again have 2 Options: 您再次有2个选择:

  1. Lazily: use the static holder pattern to initialize the singleton lazily the first time it is used 懒惰:第一次使用静态持有人样式来懒惰初始化单例
  2. Eagerly: create a public static final variable in the service class and create an instance directly. 急切地:在服务类中创建一个public static final变量,然后直接创建一个实例。

The first point has it's upsides. 第一点是它的好处。 If you need to allocate resources or need to do any other "heavy" operation. 如果您需要分配资源或需要执行任何其他“繁重的”操作。 This works, and is thread safe 这有效,并且是线程安全的

For the second point you see that it's like the second point from the first question. 对于第二点,您会看到这就像第一个问题的第二点。

Conclusion 结论

As said, it depends on the use case. 如前所述,这取决于用例。 I would probably use a singleton always. 我可能总是会使用单例。 Because then all the logic regarding it's state and availability are all held in one place. 因为这样所有关于它的状态和可用性的逻辑都被保存在一个地方。

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

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