Is it advantageous to create a Spring bean when I can access the only static method directly with class name
Asked Answered
D

5

18

I think my understanding of spring beans is a bit off.

I was working on my project and I was thinking about this situation.

Say I have class Foo

class Foo(){   
   public void doSomething(Object a , Object b){ // input parameters does not matter actually.
      //do something
   }
}

If I am using this class in another class like :

class Scheduler{
  ....
 @Autowired
 private Foo foo;

 someMethod(){
    foo.doSomeThind(a,b);
 }
  ....
}

In the above case Instead of Autowiring the Foo, I can make doSomeThing static and directly use Foo.doSomeThing(a,b)

I was just wondering if there any advantage of creating a bean or if there any disadvantage of using static methods like this?

If they are same, When should I go for spring bean and when should do I simply use a static method?

Dismissal answered 25/7, 2015 at 5:20 Comment(1)
If Foo does not have any state and contains only static methods, it's more of a utility class. No point making this a Spring bean since you can't really swap implementations if a class only has static methods.Moffett
T
8

Static methods are ok for small utility functions. The limitation of static code is that you can't change it's behavior without changing code itself.

Spring, on the other hand, gives you flexibility.

  1. IoC. Your classes don't know about the exact implementation of their dependencies, they just rely on the API defined by interface. All connections are specified in configuration, that can be different for production/test/other.

  2. Power of metaprogramming. You can change the behavior of your methods by merely marking them (via annotations of in xml). Thus, you can wrap method in transactions, make it asynchronous or scheduled, add custom AOP interceptors, etc.

  3. Spring can instrument your POJO method to make it an endpoint to remote web service/RPC.

http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/

Tabitha answered 25/7, 2015 at 5:43 Comment(0)
A
4

Methods in Spring beans can benefit from dependency injection whereas static methods cannot. So, an ideal candidate for static method is the one that does things more or less independently and is not envisioned to ever need any other dependency (say a DAO or Service)

Aromaticity answered 25/7, 2015 at 5:35 Comment(0)
Q
2

People use Spring not because of some narrow specific futures that cannot be replaced by static classes or DI or whatever. People use Spring because of a more abstracted features and ideas it provide out of the box. Here is a nice quote from Someone`s blog:

Following are some of the major benefits offered by the Spring Framework:

  • Spring Enables POJO Programming. Spring enables programmers to develop enterprise-class applications using POJOs. With Spring, you are able to choose your own services and persistence framework. You program in POJOs and add enterprise services to them with configuration files. You build your program out of POJOs and configure it, and the rest is hidden from you.
  • Spring Provides Better Leverage. With Spring, more work can be done with each line of code. You code in a more fast way, and maintain less. There’s no transaction processing. Spring allows you to build configuration code to handle that. You don’t have to close the session to manage resources. You don’t have to do configuration on your own. Besides you are free to manage the exceptions at the most appropriate place not facing the necessity of managing them at this level as the exceptions are unchecked.
  • Dependency Injection Helps Testability. Spring greatly improves your testability through a design pattern called Dependency Injection (DI). DI lets you code a production dependency and a test dependency. Testing of a Spring based application is easy because all the related environment and dependent code is moved into the framework.
  • Inversion of Control Simplifies JDBC. JDBC applications are quite verbose and time-taking. What may help is a good abstraction layer. With Spring you can customize a default JDBC method with a query and an anonymous inner class to lessen much of the hard work.
  • Spring’s coherence. Spring is a combination of ideas into a coherent whole, along with an overall architectural vision to facilitate effective use, so it is much better to use Spring than create your own equivalent solution.
  • Basis on existing technologies. The spring framework is based on existing technologies like logging framework, ORM framework, Java EE, JDK timers, Quartz and other view related technologies.
Quinquevalent answered 25/7, 2015 at 5:54 Comment(0)
W
0

It actually depends on the role of the component you are referring to: Is this feature:

  • An internal tooling: you can use static (you wouldn't wrap Math.abs or String.trim in a bean)
  • Or a module of the project: design it to be a bean/module-class (a DAO class is best modular to be able to change/mock it easily)

Globally, you should decide w.r.t your project design what are beans and what are not. I think many dev put too much stuff inside bean by default and forget that every bean is an public api that will be more difficult to maintain when refactoring (i.e. restrained visibility is a good thing).

In general, there are already several answers describing the advantages of using spring beans, so I won't develop on that. And also note that you don't need spring to use bean/module design. Then here are the main reasons not to use it:

  1. type-safety: Spring bean are connected "only" at runtime. Not using it, you (can) get much more guaranties at compile time
  2. It can be easier to track your code as there is no indirection due to IoC
  3. You don't need the additional spring dependency/ies which get quite heavy

Obviously, the (3) is correct only if you don't use spring at all in your project/lib.

Also, The (1) and (2) really depend on how you code. And the most important is to have and maintain a clean, readable code. Spring provides a framework that forces you to follow some standard that many people like. I personally don't because of (1) and (2), but I have seen that in heterogeneous dev teams it is better to use it than nothing. So, if not using spring, you have to follow some strong coding guidelines.

Wildebeest answered 25/6, 2019 at 13:29 Comment(1)
Why the -1 ? Please explain yourself so that I can improve my answerWildebeest
W
0

During unit testing you have more flexibility using bean because you can easily mock your bean methods. However, that is not the same with static methods where you may have to resort to PowerMock (which I recommend you stay away from if you can).

Whitebait answered 11/4, 2020 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.