Labs ICT
โญ Pro Login

Spring Beans

Objects managed by the Spring container.

Spring Beans

Alright, let's talk about Spring Beans. In Spring, a bean is just a Java object that's managed by the Spring IoC container. Think of beans as the building blocks of your application. When you create a class and tell Spring to manage it, that class becomes a Spring Bean.

Why does this matter? Because when Spring manages your objects, it can handle all the boilerplate code for you. Things like creating instances, managing dependencies, and handling lifecycle events. You focus on the business logic, Spring handles the rest.

Creating a Spring Bean is simple. You just annotate your class with one of Spring's stereotype annotations like @Component, @Service, or @Repository. Spring will then create and manage instances of that class automatically.

The Spring container scans your classpath for these annotations and creates beans during application startup. It's like having a factory that automatically builds and assembles all the components your application needs.

Bean Lifecycle

Every bean goes through a lifecycle. First, Spring creates the instance. Then it sets any dependencies. Then it calls any initialization methods you've specified. Finally, when the application shuts down, Spring destroys the bean. This lifecycle gives you hooks to run custom code at different points.

You can use annotations like @PostConstruct and @PreDestroy to run code after bean creation and before destruction. It's like having a welcome party when a new employee joins and a farewell party when they leave.

@Component
public class DatabaseConnection {
    
    @PostConstruct
    public void init() {
        System.out.println("Connection established");
    }
    
    @PreDestroy
    public void cleanup() {
        System.out.println("Connection closed");
    }
}
Try it Yourself โ†’

Bean Scopes

By default, Spring creates beans as singletons. This means there's only one instance of that bean in the entire application. Every time you request that bean, you get the same instance. It's efficient and works well for most cases.

But sometimes you need a new instance each time. That's where the prototype scope comes in. You can specify @Scope("prototype") to tell Spring to create a new instance for every request. It's like having a personal printer versus a shared office printer.

There are also web-specific scopes like request and session. Request scope creates a new bean for each HTTP request, while session scope creates one per user session. These are perfect for web applications where you need to manage state across different interactions.

How Spring Creates Beans

Spring uses reflection to create beans. It reads your annotations, determines what dependencies are needed, and wires everything together. This happens during application startup, so by the time your application is running, all beans are ready to use.

You can also define beans using @Bean methods in @Configuration classes. This gives you more control over how beans are created. It's useful when you need to configure third-party libraries or have complex initialization logic.

Understanding beans is fundamental to Spring Boot. They're the foundation of everything โ€“ from your controllers to your repositories. Once you're comfortable with how beans work, you'll see Spring Boot as the elegant framework it truly is.

๐Ÿงช Quick Quiz

What is a Spring Bean?