🔥 Launch Offer: Interview Preparation Bundle @ ₹2,499 ₹9,999 • 75% OFF. Offer expires in 🔥 Launch Offer: Interview Preparation Bundle @ ₹2,499 ₹9,999 • 75% OFF. Offer expires in Interview Preparation Bundle @ ₹2,499 ₹9,999

00:00:00 Get Bundle
home spring boot springapplication-run-applicationcontext-beanfactory-component-scan-dependency-injection
Interview guide Spring Boot 41 sections

SpringApplication.run(), ApplicationContext, BeanFactory, Component Scan, Dependency Injection

1. SpringApplication.run() SpringApplication.run() is the entry point of a Spring Boot application. It starts the application, creates the Spring container (ApplicationContext), performs component scanning, creates beans, injects dependencies,…

Start reading
By javainterviewprep June 10, 2026 8 min read

1. SpringApplication.run()

SpringApplication.run() is the entry point of a Spring Boot application. It starts the application, creates the Spring container (ApplicationContext), performs component scanning, creates beans, injects dependencies, and starts the embedded server (like Tomcat).

The key is to understand that SpringApplication.run() does not do everything itself—it orchestrates(arrange or coordinate) everything. It delegates work to other Spring components.

Think of it like this:

  • SpringApplication.run() = Project Manager
  • ApplicationContext = Factory Manager
  • Component Scan = Recruitment Team
  • BeanFactory = Object Factory
  • Dependency Injection = HR assigning employees to teams

The project manager doesn’t personally recruit employees or build products. It coordinates the teams that do.

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

What happens internally?

When SpringApplication.run() is executed, Spring Boot performs the following steps:

  1. Creates the appropriate ApplicationContext.
  2. Reads application configuration (application.properties or application.yml).
  3. Performs Component Scanning to find Spring-managed classes.
  4. Registers Bean Definitions.
  5. Creates singleton beans.
  6. Performs Dependency Injection.
  7. Executes Bean Lifecycle callbacks (@PostConstruct, InitializingBean, etc.).
  8. Starts the embedded web server (Tomcat/Jetty/Undertow) if required.
  9. Loads the application and makes it ready to serve requests.

Why is it needed?

Without SpringApplication.run(), Spring Boot would not:

  • Create the IoC container.
  • Manage beans.
  • Perform dependency injection.
  • Read configuration.
  • Start Tomcat.
  • Make the application ready to receive HTTP requests.

Interview Answer (30 seconds)

“SpringApplication.run() is the bootstrap method of a Spring Boot application. It creates the ApplicationContext, performs component scanning, registers and initializes beans, injects dependencies, loads configuration, executes bean lifecycle methods, and starts the embedded web server such as Tomcat. Once these steps are completed, the application is ready to process requests.”

2. ApplicationContext

ApplicationContext is the Spring IoC container that manages the complete lifecycle of beans. It is responsible for creating, configuring, storing, and managing all Spring beans throughout the application’s lifecycle.

Note: ApplicationContext is an interface, not a class. Spring Boot creates one of its implementations at runtime.

Responsibilities

The ApplicationContext is responsible for:

  • Creating beans.
  • Managing bean lifecycle.
  • Injecting dependencies.
  • Reading configuration files.
  • Managing bean scopes (Singleton, Prototype, Request, Session).
  • Publishing application events.
  • Loading properties.
  • Supporting internationalization (i18n).

How does it work?

Suppose we have:

@Service
public class UserService {
}

During startup:

  • Component Scan finds UserService.
  • ApplicationContext registers it as a bean.
  • Creates its object.
  • Stores it inside the container.
  • Returns the same object whenever another class requests it (for singleton scope).

Why is it needed?

Without ApplicationContext, developers would have to:

  • Create every object manually.
  • Manage object lifecycles.
  • Pass dependencies manually.
  • Maintain singleton instances themselves.

Spring automates all of these responsibilities.


Interview Answer

“ApplicationContext is Spring’s advanced IoC container. It creates, configures, stores, and manages all Spring beans, performs dependency injection, manages bean lifecycle, loads configuration, publishes events, and provides beans whenever required. It is an interface, and Spring Boot creates one of its implementations during application startup.”


3. BeanFactory

BeanFactory is the basic IoC container in Spring. It creates and manages beans only when they are requested (lazy initialization by default). ApplicationContext is built on top of BeanFactory and provides many additional features. It is the parent interface of ApplicationContext.

Responsibilities

BeanFactory can:

  • Create beans.
  • Manage bean lifecycle.
  • Inject dependencies.
  • Store bean instances.

Key Characteristic

Unlike ApplicationContext, BeanFactory creates beans only when they are requested (Lazy Initialization by default).

Example:

UserService service = beanFactory.getBean(UserService.class);

Only at this point will the bean be created (unless configured otherwise).


Why does Spring provide ApplicationContext?

Although BeanFactory works, enterprise applications require additional features such as:

  • Component Scanning
  • Event Publishing
  • AOP Support
  • Internationalization
  • Environment Properties
  • Annotation Processing

All these are provided by ApplicationContext.


BeanFactory vs ApplicationContext

BeanFactoryApplicationContext
Basic IoC containerAdvanced IoC container
Lazy initialization by defaultEager initialization of singleton beans by default
Minimal featuresEnterprise features
Mostly used internallyUsed in almost all Spring applications

Interview Answer

“BeanFactory is the basic IoC container in Spring responsible for creating and managing beans. It initializes beans lazily by default, meaning a bean is created only when requested. ApplicationContext extends BeanFactory and provides additional enterprise features such as component scanning, event handling, annotation support, and property management.”


4. Component Scan

Component Scan is the process where Spring automatically searches for classes annotated with @Component, @Service, @Repository, @Controller, etc., and registers them as beans in the Spring container.

Which annotations are scanned?

Spring looks for classes annotated with:

@Component
@Service
@Repository
@Controller
@RestController
@Configuration

Example:

@Service
public class UserService {
}

Spring automatically detects this class and registers it as a bean.


How does it work?

During application startup:

SpringApplication.run()
│
â–¼
Creates ApplicationContext
│
â–¼
Starts Component Scan
│
â–¼
Finds annotated classes
│
â–¼
Creates Bean Definitions
│
â–¼
Registers them in ApplicationContext

Why is it needed?

Without Component Scan:

  • Every bean would need to be configured manually.

Example:

@Bean
public UserService userService() {
return new UserService();
}

Instead, Spring automatically discovers beans.


Interview Answer

“Component Scan is Spring’s mechanism for automatically discovering classes annotated with @Component, @Service, @Repository, @Controller, and similar stereotypes. During application startup, it scans the configured packages, creates bean definitions for the discovered classes, and registers them in the ApplicationContext, eliminating the need for manual bean configuration.”


5. Dependency Injection (DI)

Dependency Injection is the process where Spring automatically provides the required objects (dependencies) to a class instead of the class creating them itself, resulting in loosely coupled and easily testable code.

Example without DI

public class UserService {

private UserRepository repository =
new UserRepository();
}

Problems:

  • Tight coupling.
  • Difficult to test.
  • Difficult to replace implementations.

With Dependency Injection

@Service
public class UserService {

private final UserRepository repository;

public UserService(UserRepository repository) {
this.repository = repository;
}
}

Spring automatically creates both objects and injects UserRepository into UserService.


Types of Dependency Injection

1. Constructor Injection ✅ (Recommended)
public UserService(UserRepository repository){
this.repository = repository;
}

Advantages:

  • Immutable dependencies.
  • Easier unit testing.
  • Ensures required dependencies are provided.
  • Recommended by Spring.

2. Setter Injection
@Autowired
public void setRepository(UserRepository repository){
this.repository = repository;
}

Useful when the dependency is optional or may change after object creation.


3. Field Injection (Not Recommended)
@Autowired
private UserRepository repository;

Simple but generally discouraged because it makes testing harder and hides dependencies.


Why is DI important?

Dependency Injection provides several benefits:

  • Loose coupling.
  • Better maintainability.
  • Easier testing and mocking.
  • Improved flexibility.
  • Better adherence to SOLID principles, especially the Dependency Inversion Principle.

Interview Answer

“Dependency Injection is the mechanism by which Spring provides required dependencies to a class instead of the class creating them itself. Spring manages the creation of beans in the IoC container and injects them where needed. Constructor injection is the preferred approach because it makes dependencies explicit, supports immutability, and simplifies testing.”


One-Line Revision

ConceptOne-Line Definition
SpringApplication.run()Starts the Spring Boot application and initializes the Spring container.
ApplicationContextThe Spring container that manages all beans and application configuration.
BeanFactoryThe basic IoC container that creates and manages beans.
Component ScanAutomatically finds annotated classes and registers them as Spring beans.
Dependency InjectionSpring automatically injects required dependencies into objects.

ApplicationContext vs Component Scan

This is a very common interview question.

ApplicationContextComponent Scan
It is the Spring IoC container.It is a process used to find Spring components.
Stores and manages all Spring beans.Searches packages for annotated classes.
Creates, configures, and manages bean lifecycle.Registers discovered classes as beans in the ApplicationContext.
Exists throughout the application’s lifetime.Runs mainly during application startup.
Provides beans whenever they are needed.Populates the ApplicationContext with beans.

In Simple Words

Component Scan = Finding the beans

  • Spring scans your project.
  • Looks for classes annotated with:
    • @Component
    • @Service
    • @Repository
    • @Controller
    • @RestController
  • These classes are identified as Spring beans.

ApplicationContext = Managing the beans

  • After Component Scan finds the beans, ApplicationContext:
    • Creates bean objects.
    • Injects dependencies.
    • Manages their lifecycle.
    • Provides them whenever required.

Flow

SpringApplication.run()
│
â–¼
Creates ApplicationContext
│
â–¼
Component Scan starts
│
â–¼
Finds @Component, @Service, @Repository...
│
â–¼
Registers them in ApplicationContext
│
â–¼
ApplicationContext creates bean objects
│
â–¼
Performs Dependency Injection
│
â–¼
Application Ready

Interview Answer (30 seconds)

Component Scan is the mechanism that scans the application’s packages to find classes annotated with Spring stereotypes like @Component and @Service. It registers those classes as bean definitions. ApplicationContext is the Spring IoC container that holds those bean definitions, creates the bean instances, injects their dependencies, manages their lifecycle, and provides them whenever the application needs them.

One-line difference:

Component Scan finds the beans; ApplicationContext manages the beans.

Relationship between IOC container and ApplicationContext

Relationship

IoC (Inversion of Control)
│
â–¼
IoC Container (Concept)
│
├── BeanFactory (Interface)
│ ▲
│ │
│ DefaultListableBeanFactory (Implementation)
│
└── ApplicationContext (Interface)
â–²
│
AnnotationConfigApplicationContext
ClassPathXmlApplicationContext
WebApplicationContext
...

What is an IoC Container?

An IoC Container is not a class or interface. It is a concept.

Its responsibilities are to:

  • Create objects (beans)
  • Manage their lifecycle
  • Inject dependencies
  • Configure beans

Spring provides implementations of this concept.


What is ApplicationContext?

ApplicationContext is an interface that represents an advanced IoC container.

It extends BeanFactory and adds features such as:

  • Component scanning
  • Event publishing
  • Internationalization (i18n)
  • Bean post-processing
  • Environment and property support

Is ApplicationContext an implementation?

No.

ApplicationContext is an interface, not an implementation.

Some implementations are:

  • AnnotationConfigApplicationContext
  • ClassPathXmlApplicationContext
  • FileSystemXmlApplicationContext
  • GenericWebApplicationContext

In a Spring Boot application, SpringApplication.run() creates an appropriate implementation of ApplicationContext (for example, AnnotationConfigServletWebServerApplicationContext for a web application).


Interview Answer

IoC Container is a concept in Spring that manages the creation, configuration, and lifecycle of objects (beans). ApplicationContext is an interface that represents an advanced IoC container. Spring Boot creates an implementation of ApplicationContext at startup to manage all application beans.

Easy Way to Remember

TermType
IoCPrinciple/Concept
IoC ContainerConcept (implemented by Spring)
BeanFactoryInterface (basic container)
ApplicationContextInterface (advanced container)
AnnotationConfigApplicationContextClass (implementation)
AnnotationConfigServletWebServerApplicationContextClass (Spring Boot web implementation)

One-line summary:

IoC Container is the concept; ApplicationContext is Spring’s advanced interface for that container, and Spring Boot creates one of its concrete implementations at runtime.