Java Spring Framework

Java 常用类库Java Common Libraries


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 core container of Spring is very compact
  • 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, etc.

Core Modules of the Spring Framework

1. Core Container

The core container is the foundation of the Spring framework, including:

  • Beans: Provides BeanFactory, implementing the Inversion of Control pattern
  • Core: Basic utility classes for the framework
  • Context: Built on 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, JPA, etc.
  • JMS: Java Message Service support
  • Transactions: Declarative transaction management

3. Web Modules

  • Web: Basic Web functionality
  • Web-MVC: 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 JUnit and TestNG testing frameworks

Core Concepts of Spring

1. Inversion of Control (IoC) and Dependency Injection (DI)

IoC is a core concept of Spring. It moves the creation of objects and the management of dependencies from application code into 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 Bean using annotations
@Component
public class MyService {
    // ...
}

// XML configuration
<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 Spring applications based on JAR packages in the classpath
  • Starter dependencies: Simplifies dependency management
  • Embedded server: No need to deploy WAR files
  • Actuator: Provides production-grade monitoring features

Create 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

  1. Modular design: You can choose which modules to use as needed
  2. Simplified development: Reduces boilerplate code through DI and AOP
  3. Test-friendly: Designed with testability in mind
  4. Powerful ecosystem: Rich third-party libraries and extensions
  5. Active community: Continuous updates and maintenance

Suggested Path for Learning Spring

  1. First master the core knowledge of Java
  2. Learn Spring Core and DI/AOP concepts
  3. Practice developing Web applications with Spring MVC
  4. Learn Spring Data for database operations
  5. Master Spring Boot to simplify the development process
  6. 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.

Java 常用类库Java Common Libraries

Other extensions