Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 957
Karthik Avatar asked Jul 25 '15 05:07

Karthik


People also ask

Can a Spring bean have static methods?

Yes, A spring bean may have static methods too.

What is the advantage of making a method static?

A static method belongs to the class rather than the object of a class. A static method can be invoked without the need for creating an instance of a class. A static method can access static data member and can change the value of it.

What are benefits of Spring beans?

Green beans are high in vitamin K, and they also contain a decent amount of calcium. These nutrients are important for maintaining strong, healthy bones and reducing your risk of fractures. Getting enough folate isn't just important during pregnancy. The B vitamin is also important for reducing depression.

When should you use @bean Spring?

@Bean is used to mark a method as one that creates a bean and Spring will then add it to the context for us. The return type of the method defines the type of bean that is created, so both of the beans created in this example will be referred to by the type MyBean rather than their implementations.


2 Answers

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/

like image 114
Aivean Avatar answered Oct 13 '22 22:10

Aivean


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)

like image 4
Wand Maker Avatar answered Oct 13 '22 21:10

Wand Maker