Static variable vs singleton

爷,独闯天下 提交于 2021-02-05 09:31:28

问题


I'm making Java REST application. 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. Is there any difference between singleton object and initializing object only once during application?


回答1:


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?

You have 2 Options:

  1. Declare a final class Services which holds all available services as public static final variables.
  2. Create in every service class a public static final variable, called 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:

  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.

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.




回答2:


You should go for Static if you want to have some utilty methods or constant variable whereas Singelton comes when a class can have state and the states can changes (but still object is one ).




回答3:


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.




回答4:


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. 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.

@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. 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.



来源:https://stackoverflow.com/questions/51514623/static-variable-vs-singleton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!