views:

48

answers:

2

I have a service in my application which is a singleton. My application is being bloated with the use of spring framework.

I am confused over to use the singleton service as

1: Plain Old Singleton [Access them statically when required]

OR as a

2: Spring singleton bean. [Use DI to inject when required]

Which approach is correct ?

A: 

The Spring singleton scope is not the same as the Singleton design pattern, which is not the same as a class with static methods.

From the documentation

"Please be aware that Spring's concept of a singleton bean is quite different from the Singleton pattern as defined in the seminal Gang of Four (GoF) patterns book. The GoF Singleton hardcodes the scope of an object such that one and only one instance of a particular class will ever be created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean. This means that if you define one bean for a particular class in a single Spring container, then the Spring container will create one and only one instance of the class defined by that bean definition."

Also, note you need to be very careful using a Spring singleton as a service that web requests will utilize. Since each request is on its own thread, you can have threading issues if your singleton maintains any state.

To answer your question: Create a class that implements an interface, and use Spring to DI it appropriately. If your service does not maintain state, you can scope it to singleton, otherwise you can scope it to prototype.

hvgotcodes
My service has no state except static reference. The spring is being used only to manage the inter BO relationships. The argument is going on to use it in other layers based on [need for managing the complex object dependencies]. Some designers think that Spring DI does not provide advantage since there are not complex inter object dependencies in those layers.
Snake
Spring is not just about DI. It is about IoC as well. And it does a lot for you. For example, you want transactions around your services right?
hvgotcodes
K in which scenarios the first approach is preferred ?
Snake
@snake using a singleton as a service is never preferred. sometimes classes with only static methods on them are used as helpers/utilities.
hvgotcodes
@hvgotcodes Can you please explain why using a singleton as a service is never preferred. We have many services designed that way.
Snake
there is no need for it, for starters. Use a singleton when you need to limit the number of objects of a type to 1. Services do not need to be limited in this way. No need to put an abstraction around service access -- its over engineering and gets you nothing. Plus, a class with static methods is not a singleton. So if you intended to use the singleton pattern, you didn't do it correctly.
hvgotcodes
@snake furthermore, by using a class with static methods as a service, you lose the ability to use a lot of the OO features of java in your services. For example you can't have an abstract service implementing an interface, with a chain of service implementations underneath that.
hvgotcodes
@hvgotcodes I think you misunderstood me. Please let me clarify,
Snake
@hvgotcodes The singleton service do not have static methods, only public methods that process input(parameters to method) and return only output produced using inputs. The singleton service has no state and only a static reference to other class to which further processing will be delegated to get the response.The reason behind singleton is - there will be only 1 instance of service that the invokers will use in whole application. I hope this will make things clear.
Snake
@snake i thought your option 1 read otherwise. My advise is still, if you are using spring, just use spring for everything you can. It is good at everything it does.
hvgotcodes
A: 

I use Spring's beans whenever possible. The framework was designed to manage these things, and it is probably better than me at it. Another reason to use Spring's dependency injection is the possibility to unit-test with mocks instead of the real utility code, thus focusing the unit-test to the exact scope.

EDIT: To answer the question in the comment, the only case I can think of for a non-bean singleton would be a utility code class, which would contain short pieces of generally reusable code in public static methods. Anything else requires instantiation, and therefore - a bean.

Yuval
K in which scenarios the first approach is preferred ?
Snake