Spring Boot Java Coding Conventions | Generated by AI
1. Core Principles
- Follow the standard Java Code Conventions
- Prioritize readability and maintainability
- Consistency across the Spring ecosystem
- Align with core Java best practices
General Guidelines
- Use IntelliJ IDEA’s default formatting
- 4-space indentation (not tabs)
- Maximum line length of 120 characters
- Use standard Java bracing style
- Blank line between methods
- No trailing whitespaces
- Use Unix-style line endings (LF)
3. Naming Conventions
Class Naming
- Use descriptive, meaningful names
- Use UpperCamelCase
- Examples:
ConfigurationProperties
AutoConfigurationImportSelector
SpringApplication
Method Naming
- Use lowerCamelCase
- Verb or verb phrases
- Examples:
configure()
registerBeanDefinitions()
isEnabledByDefault()
4. Annotation Practices
Annotation Ordering
- Standard order for annotations:
- Override annotations (
@Override
)
- Scope annotations (
@Component
, @Service
)
- Dependency injection annotations
- Transactional annotations
- Custom project annotations
Annotation Placement
@Component
@Transactional
public class UserService {
@Autowired
private UserRepository repository;
}
5. Dependency Injection
Preferred Injection Method
- Constructor injection
- Avoid field injection
- Use
@Autowired
on constructor
@Service
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
}
6. Logging
- Use SLF4J for logging
- Use
private static final Logger
or Lombok’s @Slf4j
@Slf4j
@Service
public class UserService {
public void processUser(User user) {
log.info("Processing user: {}", user.getUsername());
}
}
7. Error Handling
- Use specific exceptions
- Provide meaningful error messages
- Use
@ControllerAdvice
for global exception handling
8. Configuration
- Use
application.yml
over application.properties
- Use profile-specific configurations
- Externalize configuration values
9. Testing
- Use JUnit 5
- Leverage Spring Boot Test utilities
- Follow Arrange-Act-Assert pattern
10. Additional Resources