-4

I am just getting started with spring boot and I don't get why would I use beans instead of creating my own objects. what is the difference between an object that I created and the one spring created(bean)?

I tried making a bean and it takes more coding than creating a normal object, I had to learn how to use ApplicationContext,getBean etc..

4 Answers4

0

Short answer: Spring bean = object.

Long answer: It's easier to allow Spring to inject all of your objects into the classes that depend on them, rather then creating your own IoC container.

Read more about it here: https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/beans.html

Gabriela83
  • 53
  • 5
0

@Bean annotation is basically used to define your class as Object yes you re right that creatiing a bean initally requires a more coding that directly creating a object of the class but have you ever think suppose you have created the object by using new keyword and you have created this object at many places suppose 1000 places and if you have to change anything in your object that will complicate you because you have to go all 1000 places and change it but when you are using bean you need to make those change at single place and at all other places it will be reflectd automatically also in this case we donot need to compile your whole code that's why we use @bean also it will be very helpfull to make your code losselly coupled

0

Spring is a framework that allows you to use dependency inject. This means that the framework can build class instances (beans) for you and you simply declare the dependencies between the beans. I suggest you to get familiar with dependency injection.

@Bean
public class SuperHeavyService {

   // the datasource is build by the framework due to configuration
   private Datasource connection;
   private HttpClient client;

   // 
   public SuperHeavyService(Datasource connection, HttpClient client) {

     this.connection = connection;
     this.client = client;
   }
}

@Bean
public class HttpClient {
}

The example above shows you that the SuperHeavyService is created by the framework and the framework will inject the requires types using the constructor of the SuperHeavyService class. This is dependency injection.

saw303
  • 8,051
  • 7
  • 50
  • 90
0

We use bean mainly to achive loose coupling. When you are creating objects by yourself, then these objects are tight coupled. Imagine an application with dozens or even hundreds of classes. Sometimes we want to share a single instance of a class across the whole application, other times we need a separate object for each use case, and so on.

Managing such a number of objects is nothing short of a nightmare. This is where inversion of control comes to the rescue.

Instead of constructing dependencies by itself, an object can retrieve its dependencies from an IoC container. All we need to do is to provide the container with appropriate configuration metadata.

With the IOC, you can achieve loose coupling.

Loose coupling means that classes are mostly independent. If the only knowledge that class A has about class B, is what class B has exposed through its interface, then class A and class B are said to be loosely coupled. In order to over come from the problems of tight coupling between objects, spring framework uses dependency injection mechanism with the help of POJO/P model and through dependency injection its possible to achieve loose coupling. Example : If you change your shirt, then you are not forced to change your body – when you can do that, then you have loose coupling. When you can’t do that, then you have tight coupling. The examples of Loose coupling are Interface, JMS.

Java program to illustrate loose coupling concept

public interface Topic
{
    void understand();
}
class Topic1 implements Topic {
public void understand()
    {
        System.out.println("Got it");
    }
} class Topic2 implements Topic {
public void understand()
    {
        System.out.println("understand");
    }
} public class Subject {
public static void main(String[] args)
    {
        Topic t = new Topic1();
        t.understand();
    }
}

Explanation : In the above example, Topic1 and Topic2 objects are loosely coupled. It means Topic is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.

Hope it helps.

  • One more advantage is when we create object using new so suppose in a class in so many methods we use new and declare the class objects , from java heap point of view too it will creat so many instances , but in spring we have by default singleton class that means one object of spring bean throughout the application. – pndey Jun 13 '23 at 05:22