Mastering Spring Core – IoC & Dependency Injection (DI)

 

🌟 1. What is Spring Framework?

Spring Framework is a powerful, lightweight Java platform for building enterprise-grade applications. It provides a comprehensive infrastructure support for developing Java applications.

Main Goals:

  • Loose Coupling

  • Easy Testing

  • Better Code Maintainability

  • Integration with Web, Security, AOP, ORM, and Microservices (via Spring Boot)

🌀 2. What is Inversion of Control (IoC)?

📌 Definition:

Inversion of Control means transferring the control of object creation and dependency management to the Spring container, instead of manually creating objects using new keyword.

#BCS403,#Object Oriented Programming with Java



📦 Real-Life Analogy:

  • Without IoC: You go to a restaurant, go to the kitchen, cook food, serve yourself.

  • With IoC: You sit at a table and order. Chef (Spring container) serves you everything — you just consume.


🔧 Before Spring (Tightly Coupled Code):

public class Car {
    Engine engine = new Engine(); // tightly coupled
}

🌱 With Spring IoC (Loose Coupling):

public class Car {
    private Engine engine;

    public Car(Engine engine) { // injected via constructor
        this.engine = engine;
    }
}

And Spring container injects it for you.

🔗 3. What is Dependency Injection (DI)?

📌 Definition:

Dependency Injection is a design pattern used to achieve IoC. The object’s dependencies are injected by the container, not created manually.

✅ Types of Dependency Injection in Spring:

Type  Description    Use Case
Constructor DI  Dependencies are passed via constructor    Immutable objects
Setter DI  Dependencies injected via setter method    Optional dependencies
Field DI  Using annotations (@Autowired)    Quick setup

1️⃣ Constructor Injection (Constructor DI)

📌 Best for: Immutable dependencies (dependencies that shouldn’t change after object creation).

 Code Example:

🧩 Engine.java

 package com.example;


public class Engine {

    public void start() {

        System.out.println("🚀 Engine started via Constructor DI");

    }

}

🚗 Car.java
package com.example;

public class Car {
    private Engine engine;

    // Constructor DI
    public Car(Engine engine) {
        this.engine = engine;
    }

    public void drive() {
        engine.start();
        System.out.println("🏎️ Car is moving...");
    }
}

⚙️ applicationContext.xml

<bean id="engine" class="com.example.Engine"/>
<bean id="car" class="com.example.Car">
<constructor-arg ref="engine"/>
</bean>

▶️ Output:

🚀 Engine started via Constructor DI
🏎️ Car is moving...

2️⃣ Setter Injection (Setter DI)

📌 Best for: Optional or changeable dependencies

Code Example:

🧩 Engine.java

package com.example;
public class Engine {
public void start() {
System.out.println("🚀 Engine started via Setter DI");
}
}

🚗 Car.java
package com.example;

public class Car {
private Engine engine;

// Setter Method for Injection public void setEngine(Engine engine) { this.engine = engine; } public void drive() { engine.start(); System.out.println("🏎️ Car is moving..."); }
}

⚙️ applicationContext.xml
<bean id="engine" class="com.example.Engine"/>
<bean id="car" class="com.example.Car">
<property name="engine" ref="engine"/>
</bean>

▶️ Output:
🚀 Engine started via Setter DI
🏎️ Car is moving...

3️⃣ Field Injection (Annotation-Based @Autowired)

📌 Best for: Quick setup; not best for testing or immutability

Code Example:

🧩 Engine.java

package com.example;

import org.springframework.stereotype.Component;
@Component
public class Engine {
public void start() {
System.out.println("🚀 Engine started via Field DI");
}
}

🚗 Car.java

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Car {
@Autowired
private Engine engine; // Field Injection
public void drive() {
engine.start();
System.out.println("🏎️ Car is moving...");
}
}

📄 Java Config – AppConfig.java
package com.example;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example")
public class AppConfig {}

▶️ Main Class – App.java
package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Car car = context.getBean(Car.class);
car.drive();
}
}

▶️ Output:

🚀 Engine started via Field DI
🏎️ Car is moving...
🔍 Summary Table

DI Type Config Style Benefits        Drawbacks
Constructor DI XML / Java Config Immutable, recommended for required deps       More verbose if too many         dependencies

Setter DI XML / Java Config Good for optional properties    Allows mutation (not ideal for all cases)

Field DI Annotation-based Short and clean  Difficult to test, not           recommended for large apps

📘 4. How Spring Manages IoC and DI?

🔧 Spring provides a container called ApplicationContext, which:

  • Manages Beans

  • Injects Dependencies

  • Controls Bean Lifecycle

⚙️ 5. Let's Build a Simple Spring Core Project (Step-by-Step)
✅ Project Goal: Inject Engine object into Car using Spring

🛠 Step-by-Step (XML + Annotation Based DI)


🔶 STEP 1: Open Spring Initializr

  1. Go to: 👉 https://start.spring.io

  2. This is an official tool by Spring to generate a ready-to-use Spring Boot project.


🔶 STEP 2: Fill in Project Metadata

Fill the following fields:


Field  What to Fill
Project  Maven
Language  Java
Spring Boot  Keep latest version (e.g., 3.2.x)
Group  com.example
Artifact  spring-di-example
Name  spring-di-example
Description  Spring Boot project to demonstrate DI
Packaging  Jar
Java Version  17 or 21 (based on your system/IDE)

🔶 STEP 3: Add Dependencies

Click on the “ADD DEPENDENCIES” button and add:


  • ✅ Spring Web
  • ✅ Spring Context
  • ✅ Spring Boot DevTools (for hot reload in development)

🔶 STEP 4: Generate Project

Click the “GENERATE” button.
A .zip file will be downloaded → Unzip it on your computer.


🔶 STEP 5: Open Project in IDE

  1. Open IntelliJ IDEA, VS Code, or Eclipse.
  2. Select: File → Open → Navigate to unzipped folder → Open the folder
  3. Wait for the project to load and Maven to finish downloading dependencies.

🔶 STEP 6: Create Your Java Classes

Inside src/main/java/com/example/springdiexample/, create these files:

📁 Project Structure:

spring-core-example/
├── pom.xml
└── src/
└── main/
├── java/com/example/
│ ├── Engine.java
│ ├── Car.java
│ └── App.java
└── resources/
└── applicationContext.xml
📄 pom.xml

🔶 STEP 6: Create Your Java Classes

Inside src/main/java/com/example/springdiexample/, create these files:

📄 Engine.java
package com.example.springdiexample;

import org.springframework.stereotype.Component;
@Component
public class Engine {
public void start() {
System.out.println("🚀 Engine started via Constructor DI");
}
}

📄 Car.java
package com.example.springdiexample;

import org.springframework.stereotype.Component;

@Component
public class Car {
private final Engine engine;
// Constructor-based DI
public Car(Engine engine) {
this.engine = engine;
}
public void drive() {
engine.start();
System.out.println("🏎️ Car is moving via Spring Boot DI...");
}
}

🔶 STEP 7: Modify the Main Class

Open the auto-generated main class:

📄 SpringDiExampleApplication.java

package com.example.springdiexample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDiExampleApplication implements CommandLineRunner {
@Autowired
private Car car;
public static void main(String[] args) {
SpringApplication.run(SpringDiExampleApplication.class, args);
}
@Override
public void run(String... args) {
car.drive(); // This method runs after the app starts
}
}

🔶 STEP 8: Run Your Application

  1. Right-click on SpringDiExampleApplication.java

  2. Select Run

Output You’ll See in Console

🚀 Engine started via Constructor DI
🏎️ Car is moving via Spring Boot DI...

🎯 You Just Built a Spring Boot App!


And you used:

  • Spring Initializr to generate boilerplate code

  • @Component to register beans

  • Constructor Dependency Injection

  • @Autowired and CommandLineRunner to run code on startup






🔄 Quick Recap


StepTask
1Go to Spring Initializr
2Fill metadata and dependencies
3Generate and unzip project
4Open in IDE
5Create Engine, Car class
6Modify main class with DI
7Run and see output

🎯 Benefits of This Flow


FeatureWhy it helps?
Spring Initializr           No manual setup, production-ready base
@Component        Bean auto-registration
Constructor DI      Clean and testable
CommandLineRunner      Code runs at startup – great for testing

📚 Important Interview/Exam Questions

  1. What is Inversion of Control in Spring? Explain with example.

  2. What are the types of Dependency Injection? Which is preferred and why?

  3. Difference between BeanFactory and ApplicationContext?

  4. What is the role of @Autowired?

  5. How is Spring loosely coupled?

  6. What is the difference between XML vs Annotation based configuration?

  7. What is the life cycle of a Spring bean?

AWS Practitioner Roadmap for Beginners – Exam, Resources & Strategy Inside

 



🔰 Introduction + What Is the AWS Cloud Practitioner Exam?

“Cloud is the present, AWS is the future — and this blog series is your gateway to mastering both.”

👋 Welcome, Future Cloud Champion!

This blog series will guide you, step by step, to master everything required for the AWS Certified Cloud Practitioner (CLF-C02) exam — even if you're starting from scratch.

By the end, you'll be confident, exam-ready, and cloud-capable. 🚀

🧠 What is the AWS Cloud Practitioner Exam?

The Cloud Practitioner Certification is AWS’s foundational-level exam. It’s designed to build your understanding of the AWS Cloud — what it is, why it’s valuable, and how it works.

🧩 Key Highlights:

Feature    Details
Exam Code    CLF-C02
No. of Questions    65 (Multiple Choice & Multiple Response)
Duration     90 minutes
Passing Score    ~700/1000
Cost    $100 (approx ₹8,000)
Languages Available    English, Hindi, Japanese, and more
No Prior AWS Experience Needed   ✅

 

🧑‍🎓 Who Should Take This Exam?

This exam is ideal for:

  • Students exploring careers in cloud

  • Professionals from non-technical backgrounds (sales, marketing, finance)

  • Entry-level developers & system admins

  • Business decision-makers

  • Anyone curious about AWS without coding


📘 Official Resources (Highly Recommended)

Here are the best free and official materials by AWS:

🔹 AWS Cloud Practitioner Essentials (Free Course)
A 6-hour, beginner-friendly course directly from AWS.

🔹 AWS Skill Builder
Free platform with video courses, quizzes, and learning paths.

🔹 AWS Exam Guide (PDF)
Official document listing topics covered in the exam.

🔹 Sample Questions
A small set of real-style sample questions by AWS.

🔹 AWS Whitepapers
Especially read:

📚 Full Syllabus – CLF-C02 (Updated 2024–25)

✅ 1. Cloud Concepts – 24%

  • What is Cloud Computing?

  • Cloud Deployment Models: Public, Private, Hybrid

  • Cloud Service Models: IaaS, PaaS, SaaS

  • Benefits of AWS Cloud

  • AWS Global Infrastructure (Region, AZ, Edge Location)

  • Shared Responsibility Model

✅ 2. Security and Compliance – 30%

  • AWS Identity and Access Management (IAM)

  • IAM Roles, Policies, MFA

  • AWS Organizations and Service Control Policies

  • AWS Shield, WAF, GuardDuty

  • Data Encryption with KMS, Secrets Manager

  • AWS Compliance Programs (GDPR, HIPAA, PCI-DSS)

✅ 3. Cloud Technology and Services – 34%

  • Compute: EC2, Lambda, Elastic Beanstalk

  • Storage: S3, EBS, EFS, Glacier

  • Database: RDS, DynamoDB

  • Networking: VPC, Route 53, CloudFront

  • Monitoring: CloudWatch, CloudTrail

  • Management Tools: AWS Config, Trusted Advisor, CloudFormation

  • Hybrid and Migration Tools

✅ 4. Billing, Pricing, and Support – 12%

  • AWS Free Tier

  • Pricing Models (On-demand, Reserved, Spot)

  • TCO Calculator & AWS Pricing Calculator

  • Cost Explorer

  • AWS Support Plans (Basic, Developer, Business, Enterprise)

🎯 Preparation Strategy – How to Score 100%

⏳ Timeline: 30-Day Smart Plan (2 hours/day)

WeekFocus Area
Week 1Cloud Basics + AWS Infrastructure
Week 2Core AWS Services (EC2, S3, Lambda, VPC)
Week 3Security, IAM, Compliance
Week 4Billing, Support, Final Mocks & Revision

✅ Daily Action Plan

  • 📖 1–2 Topics with Notes

  • 🧠 Revise with Flashcards (Anki/Notion)

  • 🔍 Practice 10–15 MCQs Daily

  • 📽️ Watch Official AWS Videos

  • 📝 End-of-week full-length Mock Test


🔑 Top 5 Tips for Success

  1. Don’t memorize — understand.

  2. Practice with real AWS services (Free Tier).

  3. Attempt at least 5 mock tests before the real exam.

  4. Study from official sources first, then third-party.

  5. Revise weak topics with flashcards and visuals.


Types of Data Structures in DSA: Linear vs Non-Linear with Real-World Examples

 



🚀 Introduction

Before diving deep into algorithms, it’s crucial to understand the types of data structures that organize data efficiently. In this blog, we’ll explore:

  • The classification of data structures

  • Key differences between linear and non-linear structures

  • Real-life examples

  • And a simple code snippet to help you visualize the concept.

🧱 What is a Data Structure?

A Data Structure is a way of organizing and storing data so that it can be accessed and modified efficiently.

Just like you arrange books alphabetically on a shelf for easy access — data structures help us do the same in coding.

🧭 Classification of Data Structures

Data Structures are mainly categorized as:

Category      Subcategories
Primitive                                                  int, float, char, boolean
Non-Primitive      Linear and Non-Linear Structures


🔁 1. Linear Data Structures

In Linear structures, data is arranged in a sequential manner. Each element is connected to its previous and next element.

🔹 Common Linear Structures:

  • Array

  • Linked List

  • Stack

  • Queue

🔍 Example: Array

int[] arr = {10, 20, 30, 40};
System.out.println(arr[2]);  // Output: 30

➡️ Accessing elements by index in constant time.

🔹 When to Use Linear DS?

Use Case         Data Structure
Storing fixed-size data         Array
Frequent insertion/deletion         Linked List
Undo operations (LIFO)         Stack
Task scheduling (FIFO)         Queue

Let’s focus on Non-Primitive — the heart of DSA.

🌳 2. Non-Linear Data Structures

In Non-Linear structures, data is arranged in a hierarchical or interconnected way. Elements are not in sequence.

🔹 Common Non-Linear Structures:

  • Tree

  • Graph

  • Heap

  • Trie

🌲 Example: Binary Tree

class Node {
    int data;
    Node left, right;
}

Each node connects to two children — forming a tree structure.
Used in file systems, decision trees, and more.

🔹 When to Use Non-Linear DS?

Use Case     Data Structure
Hierarchical data (e.g. folders)     Tree
Route maps, social networks     Graph
Priority scheduling     Heap
Word prediction     Trie


🆚 Difference Table: Linear vs Non-Linear

Feature    Linear            Non-Linear
Memory Usage    Contiguous (mostly)            Non-contiguous
Data Access    Sequential            Hierarchical or Random
Examples    Array, Stack, Queue            Tree, Graph, Heap
Complexity    Easier to implement            More complex

🎯 Real-Life Applications

Structure     Real-World Use Case
Array                        List of students, temperature logs
Stack                      Browser back-button history
Queue                      Call center support system
Tree                      Folder structure in computer
Graph                      Maps and Navigation


🔚 Conclusion

Understanding the types of data structures is essential before jumping into algorithms. Whether you're building a game, a chat app, or an AI model, choosing the right data structure saves time and boosts performance.