Spring Core and Beans

Spring Core is the heart of the Spring Framework and provides fundamental features that support dependency injection and bean management. At the center of this module lies the concept of Beans, which are the objects managed by the Spring IoC (Inversion of Control) container. Understanding Spring Core and Beans is essential for developing scalable, maintainable Java applications using Spring.

What is Spring Core?

The Spring Core module offers the primary infrastructure for the framework. It includes the IoC container, which is responsible for instantiating, configuring, and assembling objects known as beans. This module is the backbone that supports other components like Spring MVC, Spring Boot, and Spring Security.

What is a Spring Bean?

A Spring Bean is an object that is instantiated, managed, and wired by the Spring container. Beans are typically defined in configuration files (XML), Java configuration classes using @Configuration, or automatically discovered using annotations like @Component, @Service, @Repository, or @Controller.

Here’s a simple example using annotations:

@Component

public class Vehicle {

    public void start() {

        System.out.println("Vehicle started");

    }

}

In a configuration class:

@Configuration

@ComponentScan("com.example")

public class AppConfig {

}

And to use the bean:

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

Vehicle vehicle = context.getBean(Vehicle.class);

vehicle.start();

The Role of the IoC Container

The IoC container in Spring is responsible for:

Creating bean instances

Managing their lifecycle

Injecting dependencies (using constructor or setter injection)

Wiring beans together based on configuration or annotations

Spring offers two major IoC containers:

BeanFactory: A lightweight container, used for simple applications.

ApplicationContext: A more advanced container that adds additional features like internationalization, event propagation, and application-layer context.

Bean Scopes in Spring

Spring provides different scopes to control the lifecycle of a bean:

singleton (default): One shared instance per Spring container

prototype: New instance every time it's requested

request, session, application: Web-specific scopes

Conclusion

Spring Core and its bean mechanism provide a powerful way to build modular, loosely coupled applications. By leveraging Spring's IoC container and bean configuration options, developers can write clean, testable code while focusing on business logic rather than infrastructure. Beans are not just objects—they are the core building blocks of a Spring application.

Learn: Java Fullstack Training In Hyderabad

Annotations in Java EE

RESTful Web Services (JAX-RS)

SOAP Web Services (JAX-WS

JavaMail API

Visit Our Quality Thought Training Institute

















 

Comments

Popular posts from this blog

Creating Microservices with Spring Boot

SOAP Web Services (JAX-WS)

File I/O and Serialization