Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the harm to make a STATIC method in service layer -- Spring 3

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)?

like image 663
Dreamer Avatar asked Nov 06 '12 17:11

Dreamer


People also ask

What is the disadvantage of static method?

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

Why static method should be avoided?

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.

What are the restrictions with static methods?

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

Is static bad practice?

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.


2 Answers

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)

like image 74
Tomasz Nurkiewicz Avatar answered Oct 12 '22 23:10

Tomasz Nurkiewicz


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.

like image 45
Alan Krueger Avatar answered Oct 13 '22 00:10

Alan Krueger