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
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.
Dependency Injection Methods
1. Constructor Injection
2. Setter Injection
3. Field Injection
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.
Spring Boot Auto-configuration
Spring Annotations
Core Annotations
Spring Configuration
Properties Configuration
Profile-based Configuration
Spring Boot Starters
Common Starters
Practical Example: User Management System
Complete Spring Boot Application
Best Practices
1. Use Constructor Injection
@Servicepublic 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
@Configurationpublic class AppConfig {@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Beanpublic ObjectMapper objectMapper() {ObjectMapper mapper = new ObjectMapper();mapper.registerModule(new JavaTimeModule());return mapper;}}
3. Use Profiles for Environment-specific Configuration
@Profile("dev")@Configurationpublic class DevConfig {// Development-specific beans}@Profile("prod")@Configurationpublic 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 objectreturn ResponseEntity.ok(userService.createUser(user));}
Summary
Spring Framework provides a comprehensive solution for Java enterprise development:
- Dependency Injection: Automatic management of object dependencies
- Spring Boot: Simplified configuration and deployment
- Auto-configuration: Automatic setup based on classpath
- Profiles: Environment-specific configuration
- Annotations: Declarative programming style
- 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
- Create a Spring Boot application with a simple REST API
- Implement dependency injection using constructor injection
- Configure different profiles for development and production
- Create a service layer with proper separation of concerns
- 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.