Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
1 views15 pages

Spring Framework Interview Guide PDF

The document provides a comprehensive guide on core Spring concepts, including loose coupling, dependency injection, and inversion of control (IOC). It explains various types of dependency injection, the roles of the IOC container, and the differences between BeanFactory and ApplicationContext. Additionally, it covers component scanning, bean scopes, and configuration options for creating application contexts in Spring.

Uploaded by

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

Spring Framework Interview Guide PDF

The document provides a comprehensive guide on core Spring concepts, including loose coupling, dependency injection, and inversion of control (IOC). It explains various types of dependency injection, the roles of the IOC container, and the differences between BeanFactory and ApplicationContext. Additionally, it covers component scanning, bean scopes, and configuration options for creating application contexts in Spring.

Uploaded by

zeeshannid12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

🍃 Spring Framework & MVC

Complete Interview Preparation Guide


Core Spring Concepts

What is Loose Coupling?

Definition: Loose coupling means that classes have minimal dependencies


on each other and can be changed independently without affecting other
classes.

Example: Instead of directly creating objects inside a class, we inject them


from outside. This way, if we need to change the implementation, we don't
need to modify the dependent class.

// Tight Coupling (Bad) class Car { private Engine


engine = new PetrolEngine(); // Direct dependency } //
Loose Coupling (Good) class Car { private Engine engine;
public Car(Engine engine) { this.engine = engine; } }

What is a Dependency?

Definition: A dependency is an object that another object needs to


function properly. When Class A uses Class B, then Class A depends on
Class B.

Example: A Car class depends on Engine class because it needs an Engine


object to work.

What is IOC (Inversion of Control)?


Definition: IOC is a design principle where the control of object creation
and lifecycle management is transferred from the application code to an
external container (like Spring).

Traditional Approach: Objects create their own dependencies


IOC Approach: Container creates and injects dependencies

Key Benefit: Reduces tight coupling and increases testability

What is Dependency Injection?

Definition: Dependency Injection is a technique where dependencies are


provided to an object from an external source rather than the object
creating them itself.

Types of Dependency Injection:


Constructor Injection: Dependencies passed through constructor
Setter Injection: Dependencies set through setter methods
Field Injection: Dependencies injected directly into fields

Can you give examples of Dependency Injection?

// Constructor Injection @Component class Car { private


final Engine engine; public Car(Engine engine) {
this.engine = engine; } } // Setter Injection @Component
class Car { private Engine engine; @Autowired public
void setEngine(Engine engine) { this.engine = engine; }
} // Field Injection @Component class Car { @Autowired
private Engine engine; }
What is Auto Wiring?

Definition: Auto Wiring is Spring's capability to automatically inject


suitable beans into other beans without explicit configuration.

Types of Auto Wiring:


byType: Matches by data type (default)
byName: Matches by variable name
Constructor: Uses constructor for injection

@Component class Engine { } @Component class Car {


@Autowired // Spring automatically injects Engine bean
Engine engine; }
IOC Container & Application Context

What are the important roles of an IOC Container?

Key Roles:
Bean Creation: Creates and manages object instances
Dependency Injection: Injects dependencies into beans
Bean Lifecycle Control: Manages creation, initialization, and
destruction
Configuration Management: Handles XML, Annotations, and Java
Config

What are Bean Factory and Application Context?

BeanFactory: Basic container that provides fundamental dependency


injection features
ApplicationContext: Advanced container that extends BeanFactory with
additional enterprise features

Can you compare Bean Factory with Application Context?

Feature BeanFactory ApplicationContext

Lazy Loading Yes No (Eager Loading)

Advanced Features No Yes (Events, AOP, etc.)

Preferred Use No Yes


Memory Usage Lower Higher

How do you create an application context with Spring?

// Java Configuration ApplicationContext context = new


AnnotationConfigApplicationContext(AppConfig.class); //
XML Configuration ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
// Web Application ApplicationContext context = new
XmlWebApplicationContext();
 
Component Scanning & Annotations

How does Spring know where to search for Components or Beans?

Spring performs component scanning to find classes annotated with


@Component, @Service, @Repository, @Controller and registers them as
beans.

What is a Component Scan?

Definition: Component scan is the process where Spring automatically


discovers and registers beans from specified packages by scanning for
stereotype annotations.

How do you define a component scan in XML and Java


Configurations?

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


Java Configuration @Configuration
@ComponentScan("com.example") public class AppConfig {
// Configuration }

How is it done with Spring Boot?

In Spring Boot, @SpringBootApplication automatically enables


component scanning for the package containing the main class and all its
sub-packages.
@SpringBootApplication // Includes @ComponentScan public
class MyApplication { public static void main(String[]
args) { SpringApplication.run(MyApplication.class,
args); } }

What does @Component signify?

@Component marks a class as a Spring-managed bean. Spring will create


and manage instances of this class.

@Component public class Engine { // Spring will manage


this class as a bean }

What does @Autowired signify?

@Autowired tells Spring to automatically inject a suitable bean from the


container into the annotated field, method, or constructor.

@Component public class Car { @Autowired private Engine


engine; // Spring injects Engine bean }

What's the difference between @Controller, @Component,


@Repository, and @Service?

Stereotype Annotations:
@Component: Generic stereotype for any Spring-managed component
@Controller: Specialization for web layer controllers (MVC)
@Service: Specialization for business logic layer
@Repository: Specialization for data access layer (includes exception
translation)

Note: All are functionally equivalent to @Component but provide semantic


meaning and additional features.
Bean Scopes & Thread Safety

What is the default scope of a bean?

The default scope is Singleton - Spring creates only one instance of the
bean and shares it across the entire application.

Are Spring beans thread safe?

No, Spring beans are NOT thread-safe by default. Since singleton beans
are shared across threads, you need to manage thread safety yourself by
avoiding mutable state or using synchronization.

What are the other scopes available?

Bean Scopes:
Singleton: One instance per Spring container (default)
Prototype: New instance every time requested
Request: One instance per HTTP request (Web)
Session: One instance per HTTP session (Web)
GlobalSession: One instance per global HTTP session

@Component @Scope("prototype") public class Engine { //


New instance created each time }

How is Spring's singleton bean different from Gang of Four Singleton


Pattern?
Aspect Spring Singleton GoF Singleton Scope Per Spring Container Per
JVM/ClassLoader Instance Creation Managed by Spring Self-
managed Multiple Instances Possible (multiple containers) Not possible
Dependency Injection Types

What are the different types of dependency injections?

Types of Dependency Injection:


Constructor Injection: Dependencies injected through constructor
Setter Injection: Dependencies injected through setter methods
Field Injection: Dependencies injected directly into fields

What is setter injection?

Setter injection involves injecting dependencies through public setter


methods after object creation.

@Component public class Car { private Engine engine;


@Autowired public void setEngine(Engine engine) {
this.engine = engine; } }

Advantages:
Allows optional dependencies
Dependencies can be changed after object creation
Supports circular dependencies

What is constructor injection?

Constructor injection involves injecting dependencies through the


constructor at object creation time.

@Component public class Car { private final Engine


engine; @Autowired // Optional from Spring 4.3 public
Car(Engine engine) { this.engine = engine; } }

Advantages:
Ensures all dependencies are provided
Creates immutable objects
Better for testing
Fails fast if dependencies missing

How do you choose between setter and constructor injections?

Use Case Recommended Injection Reason Mandatory


Dependencies Constructor Ensures dependencies are
provided Optional Dependencies Setter Allows null values Immutable
Objects Constructor Dependencies can't be changed Circular
Dependencies Setter Avoids circular reference issues

Spring Recommendation: Use constructor injection for mandatory


dependencies and setter injection for optional ones.
Spring Configuration

What are the different options available to create Application


Contexts for Spring?

Configuration Options:
XML Configuration: Traditional XML-based configuration
Java Configuration: Annotation-based configuration using
@Configuration
Spring Boot Auto Configuration: Convention-over-configuration
approach

What is the difference between XML and Java Configurations for


Spring?

Aspect XML Configuration Java Configuration Type Safety No compile-


time checking Compile-time type safety Refactoring Difficult Easy with
IDE support Learning Curve Easier for beginners Requires Java
knowledge Verbosity More verbose More concise

How do you choose between XML and Java Configurations for


Spring?

Choose Java Configuration when:


Working in a modern Java environment
Need compile-time type checking
Want better IDE support and refactoring
Prefer annotation-driven development

Choose XML Configuration when:


Working with legacy systems
Need to configure third-party libraries
Want external configuration without code changes
Team prefers declarative approach

Advanced Spring Topics

How does Spring do Autowiring?

Spring uses reflection to analyze dependencies and automatically injects


matching beans from the container based on type, name, or other criteria.

Autowiring Process:
Spring scans for @Autowired annotations
Identifies the required dependency type
Searches the container for matching beans
In

You might also like