spring boot one liner
Here’s a Spring Boot Interview One-Liners Cheat Sheet — designed for fast recall, last-minute prep, and real interview advantage, especially if you have 3–6 years of experience with Spring Boot, REST APIs, and Microservices.
⚡ Spring Boot Interview One-Liners
🔹 Core Concepts
- @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan.
- Spring Boot uses embedded servers like Tomcat, Jetty, or Undertow.
- Spring Boot follows convention over configuration.
- application.properties / application.yml → externalized configuration.
🔹 Dependency Injection (DI)
- @Component → generic Spring-managed bean.
- @Service → service-layer bean.
- @Repository → persistence-layer bean with exception translation.
- @Controller vs @RestController: the latter = @Controller + @ResponseBody.
🔹 REST API Development
- @GetMapping, @PostMapping, etc. map HTTP methods.
- @RequestParam → for query parameters.
- @PathVariable → for URL path variables.
- @RequestBody → bind JSON to Java object.
- @ResponseEntity → customize status code and body in response.
🔹 Configuration & Properties
- Use @Value("${property.name}") to inject property.
- Use @ConfigurationProperties(prefix = "app") to bind multiple config values to a bean.
- Profiles: @Profile("dev") activates a bean for a specific environment.
- Activate profile with spring.profiles.active=dev.
🔹 Spring Boot Annotations
- @EnableAutoConfiguration scans the classpath and auto-configures beans.
- @Bean declares a Spring-managed bean manually.
- @Primary marks a bean as the default when multiple candidates exist.
- @ConditionalOnProperty conditionally loads a bean based on property value.
🔹 Data Access (Spring Data JPA)
- JpaRepository provides CRUD methods out of the box.
- Method names like findByEmailAndStatus() auto-generate queries.
- @Query lets you write custom JPQL or native queries.
- Transactions are handled via @Transactional.
🔹 Exception Handling
- @ControllerAdvice handles exceptions across the whole app.
- @ExceptionHandler(SomeException.class) maps specific exception handlers.
- Return ResponseEntity for flexible status + body responses.
🔹 Actuator & Monitoring
- Spring Boot Actuator exposes production-ready endpoints like /actuator/health.
- Enable endpoints via management.endpoints.web.exposure.include=*.
- Use for health checks, metrics, and tracing.
🔹 Security (Spring Security Basics)
- WebSecurityConfigurerAdapter is deprecated in Spring Security 5.7+; use SecurityFilterChain bean.
- @PreAuthorize("hasRole('ADMIN')") secures method-level access.
- Spring Security uses UserDetailsService for custom authentication logic.
- CSRF protection is enabled by default (disable for APIs if needed).
🔹 Dev Tools & Productivity
- spring-boot-devtools enables hot reload during development.
- @SpringBootTest loads the entire application context for integration tests.
- @WebMvcTest focuses only on web layer (controllers).
🔹 API Versioning
- Common methods: in URL path (/v1/users) or in headers (X-API-VERSION).
- Not supported out-of-box — must be handled manually or using libraries.
🔹 Application Startup
- CommandLineRunner and ApplicationRunner run code after app startup.
- Useful for initializations like database seeding or startup logs.
🔹 Profiles and Environments
- application-{profile}.properties allows profile-based config.
- Use @Profile("test") to conditionally load beans.
🔹 Embedded Server Config
- Change port: server.port=8081
- Customize context path: server.servlet.context-path=/api
- Add SSL by configuring server.ssl.* properties.
🔹 Logging
- Uses Spring Boot default logging (Logback).
- Customize with logging.level.root=DEBUG or per-package levels.
- Output format can be customized via logback-spring.xml.
🔹 Packaging & Deployment
- Spring Boot apps are self-contained JARs with an embedded server.
- Use mvn clean package or gradle bootJar to build.
- Run with java -jar target/app.jar
🔹 DevOps & Deployment Tips
- Use environment variables for secrets/configs.
- Spring Boot supports Docker and Kubernetes out-of-the-box.
- Health endpoints (/actuator/health) are useful in cloud deployments.
🔹 Best Practices
- Use DTOs to avoid exposing internal entities.
- Validate inputs using @Valid + @NotNull, @Size, etc.
- Use ModelMapper or MapStruct for converting Entity ↔ DTO.
- Use logging frameworks (Slf4j, Logback) instead of System.out.
✅ You’re now covered for:
- REST APIs
- Config, Security, and Persistence
- Error handling
- Profiles, Actuator, Deployment
- Interview-tricky annotations and behaviors
Want this in:
- 📄 PDF or Markdown format?
- 🧠Quiz format for self-practice?
- 💬 Mock interview on Spring Boot + Security + JPA?
Let me know how you’d like to use it!
Comments
Post a Comment