A typical Java/Spring architecture often follows the principles of Model-View-Controller (MVC) and incorporates various Spring modules to handle different aspects of the application. Here’s a breakdown of a typical architecture:
1. Layered Architecture
A typical Java/Spring application is structured into several layers:
a. Presentation Layer
Controller: Handles incoming HTTP requests and returns responses. Typically uses @RestController or @Controller annotations.
View: In traditional MVC, this could be JSP, Thymeleaf, or any other templating engine. In modern applications, this might be a frontend framework like React or Angular.
b. Service Layer
Service: Contains business logic. Methods are usually annotated with @Service. This layer acts as an intermediary between the controller and the repository/data access layer.
c. Data Access Layer
Repository: Handles database operations. Uses Spring Data JPA with @Repository annotation. Interacts with the database using JPA Repositories.
2. Spring Modules
a. Spring Boot
Simplifies the bootstrapping and development of new Spring applications. Provides a lot of auto-configuration.
b. Spring MVC
Handles web-related functionality. Uses annotations like @RestController, @RequestMapping, etc.
c. Spring Data
Simplifies data access. Commonly used with JPA (Java Persistence API).
d. Spring Security
Provides authentication and authorization features.
e. Spring Cloud
Useful for developing cloud-native applications. Includes modules for configuration management, service discovery, circuit breakers, routing, micro-proxy, etc.
3. Configuration
application.properties or application.yml: Configuration files for setting up database connections, server properties, and other application settings.
4. Dependency Management
Maven or Gradle: Build automation tools to manage dependencies and build the project.
Example Project Structure
code
src
└── main
├── java
│ └── com
│ └── example
│ ├── controller
│ │ └── UserController.java
│ ├── service
│ │ └── UserService.java
│ └── repository
│ └── UserRepository.java
├── resources
│ ├── application.properties
│ └── static
│ └── (static assets if any)
└── webapp
└── WEB-INF
Example Code Snippets
UserController.java
java
package com.example.controller;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userService.findById(id);
}
@PostMapping
public User createUser(@RequestBody User user) {
return userService.save(user);
}
}
UserService.java
java
package com.example.service;
import com.example.repository.UserRepository;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User findById(Long id) {
return userRepository.findById(id).orElse(null);
}
public User save(User user) {
return userRepository.save(user);
}
}