langShiftlangShift

Spring Framework Basics

Learn Spring framework core concepts, dependency injection, Spring Boot introduction and enterprise development practices

Spring Framework Basics

The Spring Framework is the core of Java enterprise development, providing a complete solution to simplify enterprise application development. As a JavaScript developer, you'll find that Spring offers functionality similar to Express.js and dependency injection in the Node.js ecosystem, but more comprehensive and powerful.

Spring Framework Overview

Core Features of Spring

  • Dependency Injection (DI): Automatic management of object dependencies
  • Aspect-Oriented Programming (AOP): Modularization of cross-cutting concerns
  • Transaction Management: Declarative transaction support
  • MVC Framework: Web application development
  • Data Access: Unified data access abstraction
Loading editor...

Dependency Injection (DI)

What is Dependency Injection

Dependency injection is a design pattern that allows objects to automatically receive their dependencies when created, without manually creating these dependencies.

Loading editor...

Dependency Injection Methods

1. Constructor Injection

Loading editor...

2. Setter Injection

Loading editor...

3. Field Injection

Loading editor...

Spring Boot Introduction

What is Spring Boot

Spring Boot is an extension of the Spring Framework that simplifies the configuration and deployment of Spring applications.

Loading editor...

Spring Boot Auto-configuration

Loading editor...

Spring Annotations

Core Annotations

Loading editor...

Spring Configuration

Properties Configuration

Loading editor...

Profile-based Configuration

Loading editor...

Spring Boot Starters

Common Starters

Loading editor...

Practical Example: User Management System

Complete Spring Boot Application

Loading editor...

Best Practices

1. Use Constructor Injection

@Service
public class UserService {
private final UserRepository userRepository;
private final EmailService emailService;
public UserService(UserRepository userRepository, EmailService emailService) {
this.userRepository = userRepository;
this.emailService = emailService;
}
}

2. Use @Configuration for Complex Beans

@Configuration
public class AppConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return mapper;
}
}

3. Use Profiles for Environment-specific Configuration

@Profile("dev")
@Configuration
public class DevConfig {
// Development-specific beans
}
@Profile("prod")
@Configuration
public class ProdConfig {
// Production-specific beans
}

4. Use @Valid for Input Validation

@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
// Spring automatically validates the user object
return ResponseEntity.ok(userService.createUser(user));
}

Summary

Spring Framework provides a comprehensive solution for Java enterprise development:

  1. Dependency Injection: Automatic management of object dependencies
  2. Spring Boot: Simplified configuration and deployment
  3. Auto-configuration: Automatic setup based on classpath
  4. Profiles: Environment-specific configuration
  5. Annotations: Declarative programming style
  6. Starters: Pre-configured dependencies

As a JavaScript developer, you'll find that Spring offers similar functionality to Node.js frameworks but with stronger typing, better dependency management, and more comprehensive enterprise features.

Exercises

  1. Create a Spring Boot application with a simple REST API
  2. Implement dependency injection using constructor injection
  3. Configure different profiles for development and production
  4. Create a service layer with proper separation of concerns
  5. Implement input validation using @Valid annotation

Next Steps

In the next module, we'll explore Spring MVC for web development, including controllers, request mapping, and view resolution.