LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 118 min read

Introduction to Spring Framework

Learn what the Spring Framework is, why it exists, and how Inversion of Control and Dependency Injection make Java applications easier to build and test.

Introduction

If you have written any amount of enterprise Java, you have probably heard the word "Spring" mentioned constantly. Spring Framework is the most widely used application framework in the Java ecosystem, powering everything from small internal tools to massive banking and e-commerce backends. This lesson introduces what Spring actually is, the problem it was built to solve, and gives you a first taste of its core idea before we go deeper in later lessons.

What You Will Learn
  • What the Spring Framework is and what it is not.
  • The pain points in plain Java that led to Spring's creation.
  • Inversion of Control (IoC) and Dependency Injection (DI) in one sentence each.
  • A tiny working example of Spring managing an object for you.

What is Spring Framework?

Spring is an open-source application framework for Java that provides infrastructure support so you can focus on writing business logic instead of plumbing code. It is not a single tool but a large family of modules — for building web applications, talking to databases, securing endpoints, handling transactions, and much more — all built on top of one core idea: a container that creates and wires your objects for you.

At its heart, Spring is a container. You describe the objects your application needs (usually with annotations), and Spring creates them, connects them together, and manages their lifecycle from startup to shutdown.

The Problem Spring Solves

Before Spring became popular, a common way to build enterprise Java applications was Enterprise JavaBeans (EJB). EJB required a lot of boilerplate — interfaces to implement, XML deployment descriptors, and a heavyweight application server just to run a simple piece of business logic. Testing was painful because your classes were tightly coupled to the container itself.

Spring, first released in 2003, offered a lighter alternative. Instead of forcing your classes to depend on framework-specific interfaces, Spring let you write plain Java classes (often called POJOs — Plain Old Java Objects) and then wired them together externally, using configuration. Your business logic stayed clean, testable, and framework-agnostic.

IoC and DI in One Sentence

The Core Idea

Inversion of Control (IoC) means your objects no longer create their own dependencies — the Spring container creates and hands them over instead; Dependency Injection (DI) is simply the technique Spring uses to do that handing-over, typically through a constructor.

That single idea — letting a container control object creation and wiring instead of doing it yourself with "new" everywhere — is the foundation for almost everything else in Spring. We will unpack it in much more depth in Lessons 6 through 10, but keep this sentence in mind as you read on.

A Tiny Spring Example

Here is the smallest possible taste of Spring in action. We define a simple service class, mark it so Spring knows to manage it, and then ask Spring for an instance instead of creating one ourselves with "new".

Greeter.java
package com.programinds.demo;
import org.springframework.stereotype.Component;
@Component
public class Greeter {
public String greet(String name) {
return "Hello, " + name + "! Welcome to Spring.";
}
}
AppConfig.java
package com.programinds.demo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.programinds.demo")
public class AppConfig {
}
MainApp.java
package com.programinds.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
Greeter greeter = context.getBean(Greeter.class);
System.out.println(greeter.greet("Developer"));
}
}
Console Output

Click Run to see what this code prints.

Notice that we never wrote "new Greeter()" ourselves. We annotated the class with @Component, told Spring where to look for such classes with @ComponentScan, and then asked the container for the object with getBean(). Spring created the Greeter instance for us — that is Inversion of Control in practice.

Where Spring Fits in Java

Spring sits on top of core Java and works alongside the standard library, not as a replacement for it. It is commonly used for web applications (Spring MVC), data access (Spring Data), security (Spring Security), and rapid application setup (Spring Boot, which we cover in a separate course). This course focuses on Spring Core — the container, IoC, and Dependency Injection — because everything else in the ecosystem is built on top of these fundamentals.

Common Mistakes

Avoid These Mistakes
  • Thinking Spring and Spring Boot are the same thing — Spring Boot is a tool that configures Spring for you, but Spring itself is the underlying framework.
  • Assuming Spring is only for web applications — the core container works in any Java application, including desktop and batch programs.
  • Manually instantiating classes with "new" inside Spring-managed code instead of letting the container inject them, which defeats the purpose of using Spring at all.

Best Practices

  • Start with Spring Core concepts (IoC, DI, beans) before jumping into Spring Boot — it makes the "magic" much less mysterious.
  • Keep your business logic classes as plain Java objects with no direct dependency on Spring APIs wherever possible.
  • Read error messages from the ApplicationContext carefully — they usually point exactly at the misconfigured bean.

Frequently Asked Questions

Yes. Spring Framework is open-source under the Apache License 2.0 and free to use in personal and commercial projects.

Yes, a solid grasp of core Java — classes, interfaces, and annotations — is expected before starting Spring.

No. Spring is a separate, independent framework that was created partly as a reaction to the complexity of early Java EE, though the two can interoperate.

Key Takeaways

  • Spring is an application framework built around a container that creates and wires objects for you.
  • It was created to reduce the boilerplate and tight coupling found in early EJB-based Java applications.
  • Inversion of Control means the container controls object creation, not your own code.
  • Dependency Injection is the mechanism Spring uses to hand dependencies to your classes.

Summary

Spring Framework gives Java developers a lighter, more testable way to build applications by taking over object creation and wiring through its IoC container. In this lesson you saw a tiny example where Spring created a Greeter bean for you. In the next lesson, we will look at where Spring came from and how it evolved from XML configuration files to the annotation-driven style you just saw.

Next Lesson →

History & Evolution of Spring