UNIT 1 – Spring Core, IoC, DI (Exam Answers)
1. What are the main advantages of using Spring in enterprise
applications?
Spring is a Java framework used to build enterprise applications. Its main aim is to
reduce complexity and make development easier.
1. Lightweight and modular
Spring is not one big framework. We use only the parts we need.
(Like Django has apps → Spring has modules.)
2. Loose coupling using IoC and DI
Spring removes tight connection between classes.
Object creation is handled by Spring instead of the programmer.
Django-like example
In Django, we don’t manually create database connection objects; Similarly, in
Spring, the container manages object creation.
3. Easy to test
Because dependencies are injected, classes become simple to mock and test.
4. Integration with other technologies
Spring connects easily with Hibernate, JPA, Security modules, Messaging, etc.
5. Declarative programming
You can use annotations to handle transactions, security, etc.
6. Consistent architecture
It gives a uniform way of writing enterprise applications.
7. Reduces boilerplate code
Spring removes repeated and unnecessary code.
Therefore, Spring simplifies enterprise development, improves maintainability, testing,
and integration.
2. What are the main features and benefits of Spring Framework? (10 marks)
1. Inversion of Control (IoC)
Spring takes control of creating and managing objects.
Like Django ORM manages DB objects automatically.
2. Dependency Injection (DI)
Dependencies (like services, repositories) are injected into classes.
3. Aspect Oriented Programming (AOP)
Allows separating cross-cutting concerns like logging, security, transactions.
4. Transaction Management
Spring provides a simple way to handle database transactions.
5. Spring MVC
Module for building web apps (similar to Django views + URLs + templates).
6. Spring JDBC & ORM Support
Helps connect to databases easily and integrates with Hibernate/JPA.
7. Modular Architecture
You can use only the modules you need.
8. Open Source and widely used
Large community and rich documentation.
3. What are the major modules of the Spring Framework? (10 marks)
Spring is divided into multiple modules. Some important ones:
1. Core Container Module
● spring-core: IoC and DI
● spring-beans: Bean creation
● spring-context: Application context
● spring-expression: Expression Language (SpEL)
2. Data Access / Integration Module
● spring-jdbc: JDBC support
● spring-tx: Transaction management
● spring-orm: ORM integration
● spring-jms: Messaging
3. Web Module
● spring-web: Basic web features
● spring-webmvc: Spring MVC framework (like Django views)
4. AOP Module
Handles aspect-oriented programming (logging, security).
5. Test Module
Helps test Spring applications.
These modules allow developers to pick only what they need, making Spring flexible and lightweight.
4. How do you configure the IoC container using Java-based configuration
(@Configuration, @Bean)?
A. @Configuration
● Purpose: Marks a class as a source of bean definitions. Spring reads this class to
understand what objects to create.
● Concept Parallel (Django): Think of this as your main [Link] file where you
define all the components (like installed apps, database, etc.) your project needs.
B. @Bean
● Purpose: Annotated on a method within a @Configuration class. The method's
return value is registered as a Bean in the Spring container. The method name is
typically the bean ID.
● Concept Parallel (Django): Like a function in [Link] that initializes and returns
a complex object (e.g., a custom logger or cache backend) for Django to use.
Imagine you have a class called CustomerService (like a Django Service/Helper class) that
needs a DatabaseConnector (like a Django ORM connection).
5. What is Dependency Injection in Spring? Types of DI? (10 marks)
Definition
Dependency Injection (DI) means passing the dependent object to a class instead of the
class creating it itself.
Spring's IoC container automatically injects required objects.
Why DI?
● Reduces coupling
● Makes testing easier
● Promotes clean code
[Link] is Auto Scanning? How do @Component, @Service, @Repository,
@Controller work with @ComponentScan? (10 marks)
What is Auto Scanning?
Auto-scanning means Spring automatically finds and creates beans from classes
annotated with specific annotations.
No need to define beans manually.
Key Annotations
Annotation Purpose
@Component Generic Bean
@Service Service layer class
@Repository DAO/database class
@Controller Web MVC controller
All of the above are special types of @Component.
Django-like analogy
It is similar to Django automatically discovering apps inside
INSTALLED_APPS.
You don’t manually load each model; Django auto-detects them.