application.properties
Common Properties
# Server
server.port=8080
server.servlet.context-path=/api
# Database
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=user
spring.datasource.password=pass
spring.jpa.hibernate.ddl-auto=update
# Logging
logging.level.root=INFO
logging.level.com.example=DEBUG
logging.file.name=app.log
# Jackson (JSON)
spring.jackson.serialization.write-dates-as-timestamps=false
spring.jackson.default-property-inclusion=non_null
Externalized Configuration
# Override via command line
java -jar app.jar --server.port=9090
# Override via environment variable
SERVER_PORT=9090 java -jar app.jar
# Override via profile
java -jar app.jar --spring.profiles.active=prod
Property Precedence (highest to lowest)
- Command line arguments
- System properties
- Environment variables
- Profile-specific properties
- application.properties
- Default properties
Best Practices
Key Principles
- Follow SOLID principles
- Write clean, readable code
- Test thoroughly
- Document decisions
- Monitor in production
Implementation
- Start simple, refactor as needed
- Use established patterns
- Consider trade-offs
- Review with peers
Continuous Improvement
- Learn from incidents
- Update documentation
- Share knowledge
- Mentor others
Key Points
- Understanding application.properties is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for application.properties in a backend system. Consider scalability, error handling, and production readiness.
Solution
// application.properties implementation
// Key aspects: validation, error handling, logging, testing
public class applicationproperties {
// Production-ready implementation
} Identify and handle edge cases for application.properties. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency Write a testing strategy for application.properties. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection Quiz
1. How do you override a property via environment variable?
2. What is the highest precedence for property override?
3. What is a common mistake when implementing application.properties?
Flashcards
Question
Highest property precedence?
Click to reveal answer
Answer
Command line arguments (--server.port=9090)
Question
Environment variable format?
Click to reveal answer
Answer
UPPER_CASE: server.port → SERVER_PORT
Question
application.properties best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. application.properties configures Spring Boot
- 2. Externalize: command line, env vars, profiles
- 3. Property precedence: CLI > System > Env > Profile > properties
- 4. Use profiles for environment-specific config
Interview Tips
- • Know how to override properties
- • Understand property precedence
Cheat Sheet
application.properties
- Server: server.port, server.servlet.context-path
- DB: spring.datasource., spring.jpa.
- Override: CLI args > env vars > profiles > properties
- Env vars: server.port → SERVER_PORT