This project was developed with a focus on scalability, maintainability, and productivity in enterprise application development, by applying Domain-Driven Design (DDD) and Clean Architecture principles.
- π― Overview
- ποΈ Architecture
- π¨ Generic Structures
- πΎ Databases
- π§ Configuration and Installation
- π Execution
- π Execution Profiles
- π Monitoring and Logs
- π Swagger/OpenAPI
- π§ͺ Tests
- π€ Contributing
This template was carefully designed to accelerate backend application development following software engineering best practices. With a solid architecture based on DDD and Clean Architecture, it offers:
- Highly reusable generic structures for CRUD operations
- Robust error handling system with automatic logging
- Advanced monitoring with AOP (Aspect-Oriented Programming)
- Multi-database support (PostgreSQL, MongoDB, Neo4j)
- Flexible configuration with multiple execution profiles
- Automatic documentation with Swagger/OpenAPI
The project follows a well-defined structure that clearly separates responsibilities:
src/main/java/com/template/app/
βββ _shared/ # Shared components
β βββ application/ # Generic application services
β βββ domain/ # Generic domain entities
β βββ infrastructure/ # Generic infrastructure
βββ modules/ # π― BUSINESS LOGIC
β βββ [your_modules]/ # Specific domain contexts
βββ configuration/ # Spring configurations
βββ exception/ # Global exception system
βββ logging/ # Logging system
βββ utils/ # Utilities
- Separation of Concerns: Each layer has a specific responsibility
- Dependency Inversion: Domain doesn't depend on infrastructure
- Code Reuse: Generic structures reduce duplication
- Testability: Architecture facilitates unit test creation
The template uses a hierarchical entity system that maximizes code reuse:
GenericClass // Base entity for all domains
βββ GenericBusinessClass // For business entities
βββ [YourEntity] // Your specific entities
GenericEntity // Base JPA entity
βββ GenericBusinessEntity // For business entities with JPA
βββ [YourEntitySchema] // Your specific database schemas
The repository system offers complete CRUD operations:
// Generic interface with all operations
GenericBusinessRepository<E>
// Implementation with advanced features
GenericBusinessRepositoryImpl<E, S>
Included features:
- β Synchronous and asynchronous CRUD operations
- β Paginated queries
- β Automatic soft delete
- β Change auditing
- β User/company access control
Service layer with pre-implemented functionality:
// Generic service with basic operations
GenericServiceImpl<E, R>
Available resources:
- π Automatic authentication via MDC
- π‘οΈ Standardized exception handling
- π Optimized read operations
- β‘ Asynchronous operation support
The template comes pre-configured with three databases for different needs:
- Usage: Transactional and relational data
- Port: 5432
- Configuration: JPA/Hibernate with schema validation
- Usage: System logs, auditing and analytics
- Port: 27017
- Configuration: Spring Data MongoDB
- Usage: Complex relationships and graph analysis
- Ports: 7474 (HTTP), 7687 (Bolt)
- Configuration: Spring Data Neo4j
Run all databases with one command:
docker-compose up -d
Included services:
- PostgreSQL + PgAdmin (port 5050)
- MongoDB
- Neo4j
- Java 21+
- Maven 3.8+
- Docker & Docker Compose
- Clone the repository:
git clone https://github.com/your-user/spring-boot-ddd-template.git
cd spring-boot-ddd-template
- Configure environment variables:
cp .env.example .env
# Edit the .env file with your configurations
- Start the databases:
docker-compose up -d
- Run the project:
mvn spring-boot:run
# Default profile (development)
mvn spring-boot:run
# Or specifying the profile
mvn spring-boot:run -Dspring-boot.run.profiles=dev
mvn clean package -Pprod
java -jar target/app-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod
mvn test -Ptest
The project supports multiple profiles for different environments:
File: application-dev.yml
spring:
application:
name: app-dev
jpa:
show-sql: true
hibernate:
ddl-auto: validate
Characteristics:
- Detailed logging enabled
- Database schema validation
- Configurations optimized for development
File: application-prod.yml
spring:
application:
name: app-prod
jpa:
hibernate:
ddl-auto: validate
Characteristics:
- Performance-optimized configurations
- Minimal logging
- Strict schema validation
- Environment variables for sensitive configurations
File: application-test.yml
spring:
application:
name: app-test
jpa:
hibernate:
ddl-auto: update
Characteristics:
- In-memory H2 database
- Auto table creation
- Isolated test configurations
Each profile can be configured by editing its respective YAML file:
# Example of environment-specific configuration
server:
port: ${SERVER_PORT:8080}
spring:
datasource:
url: ${SPRING_DATASOURCE_URL}
username: ${SPRING_DATASOURCE_USERNAME}
password: ${SPRING_DATASOURCE_PASSWORD}
The template includes an advanced monitoring system based on AOP:
- Automatic monitoring of all methods in business modules
- Exception capture with complete context
- Asynchronous logging to not impact performance
- Call tracking with user/company information
@Around("execution(* com.template.app.modules..application..*(..))")
public Object domainMonitor(ProceedingJoinPoint joinPoint) throws Throwable {
// Automatically monitors all application methods
}
- ErrorLogSchema: Error logs with complete stack trace
- MethodCallLogSchema: Method call logs
- RequestLogSchema: HTTP request logs
All logs are stored in MongoDB for:
- π Performance analysis
- π Advanced debugging
- π Usage metrics
- π Complete audit
Robust exception handling system:
@RestControllerAdvice
public class GlobalExceptionHandler {
// Captures ALL system exceptions
// Saves logs automatically
// Returns standardized responses
}
Features:
- β Automatic capture of all exceptions
- β Asynchronous logging for performance
- β Standardized responses for the client
- β Sensitive information filtering
The project generates automatic API documentation using OpenAPI 3.0:
Access: http://localhost:8080/api/swagger-ui.html
@OpenAPIDefinition(
info = @Info(title = "Backend API", version = "v1"),
tags = {
// Tags organized by business context
}
)
@SecuritySchemes({
@SecurityScheme(
name = "BearerAuth",
type = SecuritySchemeType.HTTP,
scheme = "bearer",
bearerFormat = "JWT"
)
})
- π JWT Authentication integrated in documentation
- π Organized tags by business context
- π§ͺ Direct testing of APIs via interface
- π Export in standard OpenAPI formats
The template includes base classes to facilitate test creation:
// Generic test for repositories
GenericBusinessRepositoryTest<E>
// Base configuration for tests
GenericTest
# All tests
mvn test
# Specific test profile
mvn test -Ptest
# Tests with coverage
mvn test jacoco:report
- β TestContainers for PostgreSQL
- β H2 for fast tests
- β Pre-configured mocks
- β Automated test data
- Create the module structure in
src/main/java/com/template/app/modules/[your_module]/
your_module/
βββ application/ # Application services
β βββ interfaces/ # Service interfaces
β βββ impl/ # Implementations
βββ domain/ # Domain entities
βββ infrastructure/ # Repositories and adapters
βββ repository/
βββ mapper/
- Extend generic classes:
// Domain
public class YourEntity extends GenericBusinessClass {
// Your specific fields
}
// Repository
public class YourRepository extends GenericBusinessRepositoryImpl<YourEntity, YourSchema> {
// Specific implementations
}
// Service
public class YourService extends GenericServiceImpl<YourEntity, YourRepository> {
// Specific business logic
}
- Configure in
application-{profile}.yml
- Add dependency in
pom.xml
- Create Spring configuration in
configuration/data/
- Create your specific exception in
exception/models/
- Add handling in
GlobalExceptionHandler
- Fork the project
- Create a branch for your feature (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Follow SOLID principles
- Keep test coverage above 80%
- Use Javadoc to document public methods
- Follow established naming conventions
Use the available issue templates for:
- π Report bugs
- π‘ Suggest features
- π Improve documentation
This project is licensed under the MIT License - see the LICENSE file for details.
Lucas Batista Pereira
β If this template was useful to you, consider giving it a star in the repository!
Developed with β€οΈ for the Java/Spring Boot community