09 December 2023

Creating and Managing Beans in Spring Boot: A Beginner's Guide

 Spring Boot, a part of the larger Spring Framework, simplifies the process of building production-ready applications with Spring. In this guide, we'll explore the basics of creating and managing beans in Spring Boot, using Indian names for clarity and relatability.


What is a Bean in Spring Boot?

In the context of Spring Boot, a bean is an object that is instantiated, assembled, and managed by the Spring IoC (Inversion of Control) container. Beans are the building blocks of a Spring application, providing modularity and easy integration.

Creating a Simple Bean in Spring Boot

Let's create a simple bean representing a person with an Indian name. We'll call it Vyakti (meaning person in Hindi). The Vyakti bean will have properties such as naam (name), umar (age), and patrika (ID card).


import org.springframework.stereotype.Component;

@Component
public class Vyakti {
private String naam;
private int umar;
private String patrika; // Default constructor
public Vyakti() {
// Initialize the bean
} // Getter and setter methods for naam
public String getNaam() {
return naam;
} public void setNaam(String naam) {
this.naam = naam;
} // Getter and setter methods for umar
public int getUmar() {
return umar;
} public void setUmar(int umar) {
this.umar = umar;
} // Getter and setter methods for patrika
public String getPatrika() {
return patrika;
} public void setPatrika(String patrika) {
this.patrika = patrika;
}
}


Here, Vyakti is annotated with @Component, indicating that it is a Spring-managed bean.


Managing Beans in Spring Boot
Spring Boot simplifies bean management, and it can automatically detect and instantiate beans. Let's see how we can use our Vyakti bean in a Spring Boot application.

Application Class

Create a Spring Boot application class, for example, VyaktiApplication.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class VyaktiApplication {

public static void main(String[] args) {
// Start the Spring Boot application
ApplicationContext context = SpringApplication.run(VyaktiApplication.class, args);

// Retrieve the Vyakti bean
Vyakti vyakti = context.getBean(Vyakti.class);

// Display the information System.out.println("Vyakti Details:"); System.out.println("Naam: " + vyakti.getNaam()); System.out.println("Umar: " + vyakti.getUmar()); System.out.println("Patrika: " + vyakti.getPatrika()); }
}

In this example, the @SpringBootApplication annotation includes the @ComponentScan annotation, which enables component scanning to detect and register the Vyakti bean.


Conclusion

Creating and managing beans in Spring Boot is straightforward, and it plays a crucial role in building well-structured applications. By using annotations like @Component, Spring Boot simplifies the configuration and allows for easy integration of beans.


This simple example using Indian names (Vyakti) demonstrates the basic concepts of creating and managing beans in a Spring Boot application. As you continue your journey with Spring Boot, you'll discover the power and flexibility it provides for developing robust and scalable applications. Happy coding!


No comments:

Post a Comment