Spring Boot & Java Interview Topics -
Full Chat Summary
Date: 2025-04-30
OOP Concepts
Object-Oriented Programming (OOP) is based on four main principles:
1. Encapsulation: Bundling data and methods together.
2. Abstraction: Hiding complexity and exposing only what’s necessary.
3. Inheritance: Reusing code from parent classes.
4. Polymorphism: One interface, multiple implementations.
Collections Framework – List, Set, Map; HashMap vs TreeMap; ArrayList
vs LinkedList
- List: Ordered, allows duplicates (e.g., ArrayList, LinkedList).
- Set: Unordered, no duplicates (e.g., HashSet, TreeSet).
- Map: Key-value pairs (e.g., HashMap, TreeMap).
- HashMap: Fast, unordered. TreeMap: Sorted by keys.
- ArrayList: Fast random access. LinkedList: Fast insert/delete.
Java 8+ Features – Streams, Lambdas, Optional, Functional Interfaces
- Lambdas: Inline functions using `->`.
- Streams: Functional operations on collections (filter, map, reduce).
- Optional: Avoids null checks.
- Functional Interfaces: Interfaces with one abstract method (e.g., Runnable).
Why Use Streams Instead of Lists
- Cleaner, declarative syntax.
- Supports chaining operations (map, filter, sort).
- Enables parallel processing.
- Better for transformation and filtering logic.
Immutability in Java
- Immutable objects cannot change after creation.
- Use `final` fields, no setters, and defensive copies.
- Benefits: Thread-safe, cache-friendly, predictable behavior.
Dependency Injection – Constructor vs Setter
- Dependency Injection provides dependencies from outside.
- Constructor Injection: Preferred, ensures immutability.
- Setter Injection: Used for optional dependencies.
Spring Bean Lifecycle
1. Instantiation
2. Dependency Injection
3. Aware Interfaces
4. PostConstruct / InitializingBean
5. Use Phase
6. PreDestroy / DisposableBean
Spring Bean Scopes
- Singleton: One instance per Spring container.
- Prototype: New instance every time.
- Request/Session: One per web request/session.
Spring Boot Auto-Configuration
- Enabled via @EnableAutoConfiguration (in @SpringBootApplication).
- Checks classpath, configures beans automatically.
- Can be excluded or overridden.
Spring Boot Starter Dependencies
- Pre-packaged dependencies (e.g., spring-boot-starter-web).
- Simplifies setup and version management.
@SpringBootApplication Explained
- Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
- Serves as the entry point for Spring Boot apps.
Spring Transactions – @Transactional and Propagation Types
- @Transactional makes methods transactional.
- Propagation types: REQUIRED (default), REQUIRES_NEW, NESTED, etc.
- Handles rollback on exceptions.
Repositories – CrudRepository vs JpaRepository
- CrudRepository: Basic CRUD methods.
- JpaRepository: Adds paging, sorting, and JPA-specific features.
JPQL vs Native Queries
- JPQL: Works with entity fields, portable.
- Native: Raw SQL, uses table/column names, DB-specific.
Entity Relationships – OneToMany, ManyToOne, Lazy vs Eager Loading
- OneToMany: One entity maps to many others.
- ManyToOne: Many entities refer to one.
- Lazy: Load when accessed. Eager: Load immediately.
Spring Exception Handling – @ControllerAdvice and @ExceptionHandler
- @ControllerAdvice: Global exception handler.
- @ExceptionHandler: Handles specific exceptions.
- Can return custom error response objects.
Authentication vs Authorization
- Authentication: Verifies identity.
- Authorization: Verifies access rights.
- Spring Security handles both.
Spring Security Filters – Basic, JWT, OAuth2
- Basic: Uses username/password in headers.
- JWT: Stateless, token-based auth.
- OAuth2: Delegates auth to external providers.
Microservices vs Monolith – Pros/Cons, Communication
- Monolith: One app, easier to start, harder to scale.
- Microservices: Independent, scalable, more complex.
- Communication: REST, messaging, gRPC.
When to Use REST vs Messaging
- REST: Synchronous, real-time, simpler.
- Messaging: Asynchronous, decoupled, scalable.
Resilience – Retry, Circuit Breakers (Resilience4j)
- Retry: Automatic retry for transient failures.
- Circuit Breaker: Stops repeated calls to failing services.
- Resilience4j is modern and lightweight.
REST API Design – Principles, Status Codes, Versioning
- Use HTTP verbs and resource URLs.
- Status codes: 200 OK, 201 Created, 404 Not Found, etc.
- Versioning: URI (v1), header-based, or query param.
Testing – Unit vs Integration Tests, Mockito, @WebMvcTest
- Unit: Isolated logic using mocks.
- Integration: Full app context with real beans.
- @WebMvcTest: Tests only controller layer with MockMvc.
CI/CD Basics, Dockerization, Spring Boot with Docker
- CI/CD automates build, test, and deployment.
- Docker packages the app and dependencies.
- Use Dockerfile + docker-compose to run Spring Boot in containers.
ACID Properties
- Atomicity: All or nothing.
- Consistency: Valid state before and after transaction.
- Isolation: Transactions don't interfere.
- Durability: Changes persist even after failure.