Skip to content

Spring Framework (Spring Core) project demonstrating IoC, Dependency Injection, autowiring, component scanning, and loose coupling principles.

Notifications You must be signed in to change notification settings

Aaryamanjhatakia/Spring_Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Spring Core Concepts Project ๐ŸŒฑ

This project is a pure Spring Framework (Spring Core) learning project designed to demonstrate the fundamental concepts behind Spring's Inversion of Control (IoC) container, Dependency Injection (DI), bean management, autowiring, and component scanning.

Unlike Spring Boot, this project uses the core Spring Framework directly, helping build a deeper understanding of how Spring manages objects, dependencies, and application configuration internally.

This project serves as a foundational step toward mastering Spring Boot and enterprise backend development.


๐ŸŽฏ Purpose of the Project

The primary goal of this project is to learn and demonstrate:

  • Inversion of Control (IoC)
  • Dependency Injection (DI)
  • Tight vs Loose Coupling
  • Bean configuration using XML
  • Constructor Injection
  • Setter Injection
  • Autowiring (byName, byType, constructor)
  • Annotation-based configuration
  • Component Scanning
  • Spring IoC Container behavior
  • ApplicationContext usage

๐Ÿ› ๏ธ Tech Stack

  • Java 23 (or compatible version)
  • Spring Core
  • Spring Context
  • Maven

Dependencies used:

  • spring-core
  • spring-context

๐Ÿ“ Project Structure

Spring_Project
โ”‚
โ”œโ”€โ”€ src
โ”‚   โ”œโ”€โ”€ main
โ”‚   โ”‚   โ”œโ”€โ”€ java
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚โ”€โ”€ car.example
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ bean
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ App.java
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ MyBean.java
โ”‚   โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ constructor.injection
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ App.java
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Car.java
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ Specification.java
โ”‚   โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ setter.injection
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ App.java
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ Car.java
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ Specification.java
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚โ”€โ”€ com.example
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ autowire
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ constructor
โ”‚   โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ App.java
โ”‚   โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ Car.java
โ”‚   โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ Specification.java
โ”‚   โ”‚   โ”‚       โ”‚
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ name
โ”‚   โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ App.java
โ”‚   โ”‚   โ”‚       โ”‚   โ”œโ”€โ”€ Car.java
โ”‚   โ”‚   โ”‚       โ”‚   โ””โ”€โ”€ Specification.java
โ”‚   โ”‚   โ”‚       โ”‚
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ type
โ”‚   โ”‚   โ”‚           โ”œโ”€โ”€ App.java
โ”‚   โ”‚   โ”‚           โ”œโ”€โ”€ Car.java
โ”‚   โ”‚   โ”‚           โ””โ”€โ”€ Specification.java
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚โ”€โ”€ autowired.annotations
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ App.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ AppConfig.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Employee.java
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ Manager.java
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚โ”€โ”€ componentscan.annotation
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ App.java
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ Employee.java
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚โ”€โ”€ ioc.coupling
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ IOCExample.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ NewDatabaseProvider.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserDatabaseProvider.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserDataProvider.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserManager.java
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ WebServiceDataProvider.java
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚โ”€โ”€ loose.coupling
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ LooseCouplingExample.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ NewDatabaseProvider.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserDatabaseProvider.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserDataProvider.java
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ UserManager.java
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”‚โ”€โ”€ tight.coupling
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ TightCouplingExample.java
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ UserDatabase.java
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ UserManager.java
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ””โ”€โ”€ resources
โ”‚   โ”‚       โ”œโ”€โ”€ applicationBeanContext.xml
โ”‚   โ”‚       โ”œโ”€โ”€ applicationConstructorInjection.xml
โ”‚   โ”‚       โ”œโ”€โ”€ applicationIoCLooseCouplingExample.xml
โ”‚   โ”‚       โ”œโ”€โ”€ applicationSetterInjection.xml
โ”‚   โ”‚       โ”œโ”€โ”€ autowireByConstructor.xml
โ”‚   โ”‚       โ”œโ”€โ”€ autowireByName.xml
โ”‚   โ”‚       โ”œโ”€โ”€ autowireByType.xml
โ”‚   โ”‚       โ””โ”€โ”€ componentScanDemo.xml
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ test
โ”‚
โ”œโ”€โ”€ pom.xml
โ”œโ”€โ”€ .gitattributes
โ””โ”€โ”€ README.md

๐Ÿง  Core Concepts Demonstrated


1. Inversion of Control (IoC)

In traditional programming, objects create their dependencies.

In Spring, the container creates and manages objects.

Example (Without IoC):

UserManager manager = new UserManager();

Example (With IoC):

Spring creates the UserManager object and injects dependencies automatically.

Benefits:

  • Reduces coupling
  • Improves flexibility
  • Improves maintainability

2. Dependency Injection (DI)

Dependency Injection means providing dependencies to objects instead of letting them create dependencies themselves.

Spring supports multiple types:


3. Constructor Injection

Dependencies are injected through the constructor.

Example:

public Car(Specification spec) {
    this.spec = spec;
}

Configured in XML:

<constructor-arg ref="specificationBean"/>

Benefits:

  • Ensures required dependencies are available
  • Makes objects immutable

4. Setter Injection

Dependencies are injected using setter methods.

Example:

public void setSpecification(Specification spec) {
    this.spec = spec;
}

Configured in XML:

<property name="specification" ref="specificationBean"/>

Benefits:

  • Allows optional dependencies
  • More flexible

5. Autowiring

Spring automatically injects dependencies.

Types demonstrated:


Autowire by Name

Matches bean name with property name.

autowire="byName"

Autowire by Type

Matches bean type with property type.

autowire="byType"

Autowire by Constructor

Matches constructor parameter type.

autowire="constructor"

6. Annotation-based Autowiring

Uses annotations like:

@Autowired
@Component
@Configuration

Spring automatically resolves dependencies.

Benefits:

  • Cleaner code
  • Less XML configuration

7. Component Scanning

Spring automatically detects classes annotated with:

@Component
@Service
@Repository

Configured using:

<context:component-scan base-package="com.example"/>

8. Tight Coupling

Classes depend directly on specific implementations.

Example:

UserDatabase db = new UserDatabase();

Problems:

  • Hard to change implementation
  • Not flexible

9. Loose Coupling

Classes depend on interfaces instead of implementations.

Example:

UserDataProvider provider;

Spring injects appropriate implementation.

Benefits:

  • Flexible
  • Easy to extend
  • Easier testing

10. Spring IoC Container

The container:

  • Creates objects (beans)
  • Injects dependencies
  • Manages lifecycle

Example:

ApplicationContext context =
    new ClassPathXmlApplicationContext("applicationContext.xml");

โ–ถ๏ธ How to Run the Project

Prerequisites

  • Java installed
  • Maven installed
  • IDE (IntelliJ recommended)

Build project

mvn clean install

Run example

Run any App.java file from IDE.

Example:

car.example.constructor.injection.App

or

ioc.coupling.IOCExample

๐ŸŽ“ Learning Outcome

After completing this project, you will understand:

  • How Spring IoC container works
  • How Spring manages objects
  • How Dependency Injection works internally
  • Difference between tight coupling and loose coupling
  • XML vs Annotation configuration
  • Foundation required for Spring Boot

๐Ÿ”ฎ Future Improvements

  • Add Spring Boot version of project
  • Add Spring MVC
  • Add database integration
  • Add REST APIs

๐Ÿ‘จโ€๐Ÿ’ป Author

Aaryaman Jhatakia

GitHub: https://github.com/Aaryamanjhatakia


About

Spring Framework (Spring Core) project demonstrating IoC, Dependency Injection, autowiring, component scanning, and loose coupling principles.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages