Objective
Understand the Spring Framework, its architecture, and core concepts like Inversion of Control (IoC), Dependency Injection (DI), and Spring MVC. Learn to develop web applications using Spring MVC with JSP views, explore alternative view technologies like Thymeleaf, and implement features like validations, internationalization (i18n), and file uploads.
Lecture Topics
1. What is Spring Framework
- Definition: Spring is a lightweight, modular Java framework for building enterprise applications, simplifying development by providing dependency injection, MVC support, and integration with various technologies (e.g., Hibernate, JDBC).
- Purpose:
- Reduces boilerplate code (e.g., manual object creation, configuration).
- Provides a cohesive platform for web, data access, and integration layers.
- Key Features:
- Inversion of Control (IoC) for managing object dependencies.
- Aspect-Oriented Programming (AOP) for cross-cutting concerns.
- Spring MVC for web applications.
- Integration with ORM tools, databases, and other frameworks.
2. Overview of Spring Architecture
- Modules:
- Core Container: Includes
spring-core
, spring-beans
, spring-context
, and spring-expression
.
- Provides IoC and DI functionality.
- Data Access/Integration: Includes
spring-jdbc
, spring-orm
, spring-jms
.
- Simplifies database operations and messaging.
- Web: Includes
spring-web
, spring-webmvc
.
- Supports web application development (MVC, REST).
- AOP:
spring-aop
for aspect-oriented programming.
- Test:
spring-test
for unit and integration testing.
- Architecture Layers:
- Application Layer: Business logic and controllers.
- Spring Framework: Provides services (IoC, MVC, AOP).
- Infrastructure: Integrates with databases, external systems.
- Exam Tip: Be able to list major Spring modules and their purposes.
3. Spring MVC Architecture
-
Definition: Spring MVC is a module for building web applications using the Model-View-Controller pattern.
-
Components:
- DispatcherServlet: Central Servlet that handles all HTTP requests and routes them to controllers.
- Controller: Processes requests, interacts with the model, and returns a view.
- Model: Represents data (e.g., JavaBeans, Maps) passed to the view.
- View: Renders the response (e.g., JSP, Thymeleaf).
- HandlerMapping: Maps requests to controllers.
- ViewResolver: Resolves view names to actual view templates (e.g.,
InternalResourceViewResolver
for JSP).
-
Request Flow:
- Client sends HTTP request.
DispatcherServlet
receives the request.
HandlerMapping
identifies the appropriate controller.
- Controller processes the request, interacts with the model, and returns a view name.
ViewResolver
resolves the view name to a template (e.g., JSP).
- View renders the response to the client.
-
Example Configuration (web.xml
):
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
4. Spring Modules Overview
- Core Modules: IoC container, Beans, Context, SpEL (Spring Expression Language).
- Data Access Modules: JDBC, ORM (e.g., Hibernate integration), Transactions.