Spring Framework Introduction

 The Spring Framework is an open-source framework for building Java applications, especially enterprise-level ones. It simplifies the development process by promoting good design practices like Dependency Injection (DI) and Inversion of Control (IoC).


🧩 Key Concepts

  1. Dependency Injection (DI):

    • Instead of creating objects manually, Spring "injects" the necessary dependencies into your classes, making code more modular and easier to maintain.
  2. Inversion of Control (IoC):

    • The Spring Container takes over the responsibility of managing object creation and wiring dependencies, freeing developers from this task.
  3. Beans:

    • These are the objects that Spring manages in its container.
  4. Spring Container:

    • It creates, manages, and configures beans. The container reads configuration (XML, annotations, or Java classes) to understand how to create and link objects.

🚀 Why Use Spring?

  1. Reduces Boilerplate Code:

    • Simplifies common tasks like object creation and dependency management.
  2. Modular and Flexible:

    • Use only what you need (e.g., web, data, or security modules).
  3. Easy Integration:

    • Works well with other technologies like Hibernate, JPA, and REST APIs.
  4. Robust and Scalable:

    • Suitable for applications of all sizes, from simple apps to large enterprise systems.
In Spring Framework, you can configure beans and manage dependencies using Java-based configuration and annotations. This provides a clean and flexible way to create and manage beans compared to traditional XML configuration.

Spring Boot is built on top of the Spring Framework and aims to simplify the setup and configuration of Spring applications. It provides a set of defaults and auto-configurations to help you get started quickly without needing extensive configuration.

In Spring Boot, beans can be registered in several ways, and one common approach is through annotations (e.g., @Component, @Service, @Repository) or by using a Java configuration class with @Bean annotations.

Understanding Bean Registration

In Spring, a bean is an object that is instantiated, assembled, and managed by the Spring IoC container. To register a bean, you typically annotate a class with @Component, @Service, @Repository, or @Controller. These annotations are used to scan and automatically register beans in the Spring application context.

Using @Component Annotation

The @Component annotation is the most generic annotation for registering a bean. Here's an example:

@Component
public class MyBean {
    // ...
}

Using @Service, @Repository, and @Controller Annotations

These annotations are more specific and are often used to indicate the role of a bean in the application:

  • @Service: For business logic components.
  • @Repository: For data access components.
  • @Controller: For web controllers.
@Service
public class MyService {
    // ...
}

@Repository
public class MyRepository {
    // ...
}

@Controller
public class MyController {
    // ...
}

Using CommandLineRunner to Execute Code on Startup

The CommandLineRunner interface allows you to execute code after the Spring application context is fully initialized. It's often used for initialization tasks, data loading, or running specific operations on startup.

@Component
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // Code to be executed on startup
        System.out.println("Application started!");
    }
}

Key Points to Remember:

  • Component Scanning: Spring Boot automatically scans the classpath for components annotated with @Component and its stereotypes.
  • Bean Naming: If you don't specify a bean name, Spring will generate a default name based on the class name.
  • Dependency Injection: Spring handles dependency injection automatically, injecting required dependencies into beans.
  • Configuration Properties: You can use @ConfigurationProperties to bind configuration properties to beans.
  • Profiles: You can use profiles to conditionally activate beans based on environment or other criteria.

By understanding these concepts and leveraging the power of annotations, you can effectively register beans and customize the startup behavior of your Spring Boot applications.

Comments