Java Spring Framework
Spring is an open-source Java enterprise application development framework created by Rod Johnson in 2003. It provides a comprehensive programming and configuration model for building modern Java applications.
Core features of the Spring Framework:
- Lightweight: The Spring core container is very lightweight
- Dependency Injection (DI): Manages object dependencies through the IoC (Inversion of Control) container
- Aspect-Oriented Programming (AOP): Supports modularization of cross-cutting concerns
- One-Stop Solution: Provides modules for data access, transaction management, web development, and more
Core Modules of the Spring Framework
1. Core Container
The core container is the foundation of the Spring Framework and includes:
- Beans: Provides BeanFactory and implements the Inversion of Control pattern
- Core: The framework's basic utility classes
- Context: Built on top of the Core and Beans modules, provides a framework-style way to access objects
- SpEL: Spring Expression Language
2. Data Access/Integration
- JDBC: Simplifies JDBC usage
- ORM: Supports ORM frameworks such as Hibernate and JPA
- JMS: Java Message Service support
- Transactions: Declarative transaction management
3. Web Module
- Web: Basic web functionality
- Web-MVC: A web framework implementing the MVC design pattern
- Web-Socket: WebSocket support
- Web-Flux: Reactive web framework
4. Other Important Modules
- AOP: Aspect-oriented programming support
- Aspects: AspectJ integration
- Instrumentation: Class loader implementation and class detection support
- Test: Supports the JUnit and TestNG testing frameworks
Core Concepts of Spring
1. Inversion of Control (IoC) and Dependency Injection (DI)
IoC is the core concept of Spring; it transfers the creation of objects and the management of their dependencies from application code to the container.
Example
// Traditional way
public
class
UserService
{
private
UserRepository userRepository
=
new
UserRepositoryImpl
(
)
;
}
// Spring DI approach
public
class
UserService
{
private
UserRepository userRepository
;
// Inject via constructor
public
UserService
(
UserRepository userRepository
)
{
this
.
userRepository
=
userRepository
;
}
}
2. Aspect-Oriented Programming (AOP)
AOP allows cross-cutting concerns (such as logging, transactions, security, etc.) to be separated from business logic.
Example
@Aspect
@
Component
public
class
LoggingAspect
{
@Before
(
"execution(* com.example.service..(..))"
)
public
void
logBefore
(
JoinPoint joinPoint
)
{
System
.
out
.
println
(
"Before method execution: "
joinPoint.
getSignature
(
)
.
getName
(
)
)
;
}
}
3. Spring Bean
Objects managed by the Spring container are called Beans. Beans can be defined via XML configuration or annotations.
Example
// Define a Bean using annotations
@
Component
public
class
MyService
{
// ...
}
// XML configuration approach
<
bean id
=
"myService"
class
=
"com.example.MyService"
/>
Introduction to Spring Boot
Spring Boot is an extension of the Spring Framework designed to simplify the initial setup and development process of Spring applications.
Main features of Spring Boot:
- Auto-Configuration: Automatically configures the Spring application based on the jars in the classpath
- Starter Dependencies: Simplifies dependency management
- Embedded Server: No need to deploy WAR files
- Actuator: Provides production-grade monitoring features
Creating a simple Spring Boot application:
Example
@SpringBootApplication
public
class
MyApp
{
public
static
void
main
(
String
[
]
args
)
{
SpringApplication.
run
(
MyApp.
class
, args
)
;
}
}
@RestController
class
HelloController
{
@GetMapping
(
"/hello"
)
public
String
hello
(
)
{
return
"Hello, Spring Boot!"
;
}
}
Advantages of the Spring Framework
- Modular Design: You can choose which modules to use as needed
- Simplified Development: Reduces boilerplate code through DI and AOP
- Test-Friendly: Testability was considered from the design stage
- Powerful Ecosystem: Rich third-party libraries and extensions
- Active community: Continuous updates and maintenance
Suggested Learning Path for Spring
- First master core Java knowledge
- Learn Spring Core and the DI/AOP concepts
- Practice Spring MVC to develop web applications
- Learn Spring Data for database operations
- Master Spring Boot to simplify the development workflow
- Explore Spring Cloud to build microservices
The Spring Framework is the foundation of modern Java development; mastering it will greatly improve your development efficiency and project quality.
Common Java Libraries