0% found this document useful (0 votes)
2 views4 pages

Spring Framework Day 1 Overview

The Spring Framework automates object creation and management through Inversion of Control (IoC) and Dependency Injection (DI), making code easier to maintain. Configuration classes and component scanning allow Spring to manage beans automatically, while annotations like @Service and @Repository facilitate bean creation. Dependency Injection can be achieved through field or constructor injection, with constructor injection being preferred for testing and immutability.

Uploaded by

gvarchana13
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Spring Framework Day 1 Overview

The Spring Framework automates object creation and management through Inversion of Control (IoC) and Dependency Injection (DI), making code easier to maintain. Configuration classes and component scanning allow Spring to manage beans automatically, while annotations like @Service and @Repository facilitate bean creation. Dependency Injection can be achieved through field or constructor injection, with constructor injection being preferred for testing and immutability.

Uploaded by

gvarchana13
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🌱 Spring Framework — Day 1 Notes

🔹 1. Why Spring?

 In plain Java, we manually create and manage objects — this makes the code tightly
coupled and harder to maintain.

 Example:

 CustomerService service = new CustomerService();

 These manually created objects are just POJOs (Plain Old Java Objects).

 Spring helps by automating object creation and management using the IoC (Inversion
of Control) and DI (Dependency Injection) principles.

 In short → Spring creates and manages beans for us.

🔹 2. Spring Configuration Class

Step 1: Create a central configuration class

Example:

@Configuration

public class AppConfig {

 @Configuration tells Spring that this class contains bean definitions or component
scanning logic.

Step 2: Make Spring reach this configuration

In a pure Spring (non-Boot) application, we must manually load the config class:

AnnotationConfigApplicationContext context =

new AnnotationConfigApplicationContext([Link]);

 context is the Spring container object.

 It reads the configuration and initializes all beans.

⚠️In Spring Boot, this manual step is not needed — Spring Boot automatically detects
configuration classes through component scanning.
🔹 3. Enabling Component Scanning

Add this in your config class:

@ComponentScan(basePackages = "[Link]")

 This tells Spring to scan the package and all its subpackages for components (beans).

 Classes annotated with @Component, @Service, @Repository, or @Controller will be


automatically registered as beans.

🔹 4. Creating Beans

You can create beans in two ways:

✅ Annotation-based (preferred)

Annotate your classes so Spring can auto-register them:

@Service

public class CustomerService {

@Repository

public class CustomerDao {

These annotations internally act as specialized versions of @Component.

⚙️Manual Bean Creation (alternative)

You can define beans directly inside the config class:

@Bean

public CustomerService customerService() {

return new CustomerService();

}
🔹 5. Getting Bean from Spring Container

Instead of creating objects manually:

CustomerService service = new CustomerService();

Use:

CustomerService service = [Link]([Link]);

 Spring looks up the bean from its container and injects dependencies automatically.

🔹 6. Dependency Injection (Autowired)

To connect one bean to another, use @Autowired.

Example (Field Injection):

@Service

public class CustomerService {

@Autowired

private CustomerDao customerDao;

Example (Constructor Injection — Preferred):

@Service

public class CustomerService {

private final CustomerDao customerDao;

@Autowired

public CustomerService(CustomerDao customerDao) {

[Link] = customerDao;

}
}

✅ Constructor injection is better for testing and immutability.

🔹 7. Summary

Concept Description

@Configuration Marks a class as a configuration source

@ComponentScan Scans packages for beans

@Service Marks a class as a service component

@Repository Marks a DAO layer component

@Autowired Injects one bean into another

getBean() Fetches bean from container

AnnotationConfigApplicationContext Loads Spring context manually (non-Boot)

💡 Core Idea

In Java, we control objects.


In Spring, Spring controls the objects — that’s Inversion of Control (IoC).

You might also like