A typical Java/Spring architecture follows a layered approach that promotes separation of concerns and maintainability. The most common pattern is the Model-View-Controller (MVC) architecture, often extended into a multi-tier structure.
At the presentation layer, you'll find controllers that handle HTTP requests and responses. These are typically annotated with @RestController or @Controller and manage the API endpoints. Controllers receive requests, delegate business logic to services, and return appropriate responses. In modern Spring applications, this layer often consists of RESTful APIs that return JSON data.
The service layer contains the business logic of your application. Services are annotated with @Service and act as an intermediary between controllers and data access layers. This is where you implement your core business rules, validations, and complex operations. Services are typically injected into controllers using Spring's dependency injection.
The data access layer is handled through repositories, usually annotated with @Repository. Spring Data JPA is commonly used here, providing abstractions over database operations. Repositories interface with your database and handle CRUD operations. Entity classes annotated with @Entity represent your database tables and define the object-relational mapping.
Configuration in Spring is often handled through Java-based configuration classes annotated with @Configuration, though you'll also see application.properties or application.yml files for external configuration. Spring Boot has made this even simpler with auto-configuration capabilities.
Cross-cutting concerns like security, logging, and transaction management are typically handled through Spring's Aspect-Oriented Programming (AOP) features or dedicated frameworks like Spring Security. Exception handling is often centralized using @ControllerAdvice classes.
Many Spring applications also incorporate additional components such as DTOs (Data Transfer Objects) for data transformation between layers, mappers for object conversion, filters for request preprocessing, and scheduled tasks using @Scheduled annotations. Message queues and caching layers are also common additions for more complex applications.
The entire application is typically packaged as a JAR file with an embedded server like Tomcat, making it easy to deploy as a microservice or standalone application.