I know it is not a best design but just a thought from a Spring newbie.
Now we can easily autowire
any service method to each other conveniently in Spring framework. But What is the disadvantage to create a static factory method of the service class and call it everywhere?
It's pretty common like this:
@Autowired
CustomerService customerService;
....
AccountDetail ad = customerService.getAccountDetail(accountId, type);
But this should work too:
AccountDetail ad = CustomerService.getAccountDetail(accountId, type); //if we make getAccountDetail(String, String) static
So why there is a design like autowire? It looks fancy and really cool, but the work behind this is still create one service bean instance on another service object.
Seriously while Spring is all over the market so many posts and articles are talking about pros & renovations. But is it guaranteeing better performance(like using autowire instead of static)?
in static method we cannot call normal functions that is non - static functions. The static method can not use non static data member or call non-static method directly. this and super cannot be used in static context. Access only static type data (static type instance variable).
Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous. Overriding a static method is not that simple for some languages.
A static method also has the power to access static data members of the class. The static method cannot use non-static data member or invoke non-static method directly. The this and super cannot be used in static context. The static method can access only static type data (static type instance variable).
Static Methods/Variables are bad practice. In short: Yes. There are many disadvantages and static methods should almost never be used. Static methods allow procedural/functional code to be shoe-horned into an Object Oriented world.
There are numerous reasons:
you can't replace CustomerService
with a mock easily during tests (tools like PowerMock aside)
static
methods do not participate in standard, proxy-based AOP (no transactions, security, custom aspects)
you can no longer use fancy injection techniques, like injecting HTTP request (request scoped) into singleton scoped services (poor design anyway, but...)
But to be complete, there are also advantages:
static
method is actually closer to your intent, Spring beans are very rarely stateful, thus they don't really need an instance to work
static
call might be faster (this is irrelevant in 99% of the programs)
What if you need to have multiple CustomerService components with different configuration? You can't do that with a single static method.
Also, if there's any configuration whatsoever on CustomerService, how do you inject it? Having a bean that gets wired into dependent objects centralizes your configuration and keeps you from having to hunt through your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With