Spring Boot Essentials
Spring Boot eliminates the boilerplate of traditional Spring applications. It follows a “convention over configuration” philosophy: a handful of starter dependencies gives you an embedded Tomcat server, JSON serialization, database connectivity, and more—all wired together automatically.Auto-configuration and starters
Addingspring-boot-starter-web to your pom.xml pulls in Spring MVC, an embedded Tomcat, and Jackson without any XML configuration. Spring Boot scans your classpath and conditionally activates beans based on what it finds.
application.properties:
Dependency injection
Spring Boot manages beans through its IoC container. Annotate classes with@Service, @Repository, or @Component to register them, and use @Autowired (or constructor injection) to receive dependencies:
REST controllers
Use@RestController for APIs that return data (JSON by default). @Controller is for server-rendered pages. In a front-end/back-end separated architecture you will almost always use @RestController.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping. Route paths should contain only nouns, not verbs (/users, not /getUser).
Interceptors and filters
Interceptors handle Spring-managed resources; filters run before the Spring context and intercept every request including static files. Execution order:Filter → Interceptor → Controller.
WebMvcConfigurer:
JVM Internals
The JVM manages memory in several distinct regions. Understanding them helps you tune heap sizes, diagnoseOutOfMemoryError, and reason about object lifecycles.
Memory regions
| Region | Thread scope | Purpose |
|---|---|---|
| Program Counter Register | Per-thread | Tracks the current bytecode instruction |
| JVM Stack (virtual machine stack) | Per-thread | Stores stack frames with local variables and operand stacks |
| Native Method Stack | Per-thread | Services native (C/C++) calls via JNI |
| Heap | Shared | Stores almost all object instances; managed by the GC |
| Method Area | Shared | Stores class metadata, constants, static variables, JIT-compiled code |
| Runtime Constant Pool | Shared (part of Method Area) | Literal values and symbolic references resolved at load time |
-Xms (initial size) and -Xmx (maximum size).
Object lifecycle
When the JVM executesnew Foo():
- Class load check — verifies that
Foois already loaded, linked, and initialized; triggers class loading if not. - Memory allocation — carves out a region of the heap. Uses CAS-based retry or per-thread TLAB (Thread-Local Allocation Buffer) to keep allocations thread-safe.
- Zero initialization — sets all instance fields to their zero values so code can read fields before explicitly assigning them.
- Object header setup — records the class pointer, identity hash code, GC generation age, and lock state in the header.
- Constructor — runs
<init>()to set user-defined initial values.
Class loading
Class loading proceeds through five stages:- Loading — reads the
.classbytecode and creates aClassobject in the method area. - Verification — confirms the bytecode conforms to the JVM specification (no memory corruption, no type violations).
- Preparation — allocates memory for static variables and sets them to zero (not their declared values yet).
- Resolution — replaces symbolic references in the constant pool with direct memory pointers.
- Initialization — executes the
<clinit>()method, running static initializer blocks and assigning declared static values.
Class object is GC’d, which requires all instances to be collected and the class loader itself to be collected. JVM built-in class loaders never unload their classes.
JIT compilation
The JVM interprets bytecode initially. The JIT compiler identifies hot methods and compiles them to native machine code at runtime. This means long-running Java processes typically outperform freshly started ones because the JIT has had time to optimize hot paths.Multithreading
Thread and Runnable
There are two basic ways to define work for a thread. ExtendingThread is simpler but wastes Java’s single-inheritance slot:
Runnable is preferred because it keeps your class free to extend another class and makes it easy to share one Runnable across multiple threads:
ExecutorService and thread pools
Creating and destroying threads on demand is expensive. UseExecutorService to maintain a pool of worker threads:
corePoolSize— threads kept alive even when idle.maximumPoolSize— upper bound on thread count.keepAliveTime— how long idle threads abovecorePoolSizesurvive before being terminated.
synchronized vs ReentrantLock
synchronized is an implicit lock tied to an object monitor. It releases automatically when the block exits, even on exception:
ReentrantLock is explicit and offers more control: try-lock, timed lock, and fairness settings. Always release in a finally block:
ReentrantLock > synchronized block > synchronized method.
CompletableFuture
CompletableFuture enables non-blocking async pipelines without explicit thread management:
Producer-consumer with wait / notifyAll
The classic bounded-buffer pattern uses wait and notifyAll on a shared monitor to coordinate producers and consumers:
Java 8+ Features
Optional
Optional<T> makes the possibility of a missing value explicit in the type system, eliminating entire classes of NullPointerException:
Stream API
Streams provide a declarative, functional-style pipeline for processing collections:Functional interfaces and lambdas
Any interface with exactly one abstract method is a functional interface and can be expressed as a lambda:LocalDateTime
java.time.LocalDateTime replaces the problematic Date and Calendar APIs. It is immutable and thread-safe:
MyBatis-Plus
MyBatis-Plus enhances MyBatis with generic CRUD operations, a fluent query wrapper, and pagination—without requiring you to write SQL for common cases.Setup
Add the dependencies inpom.xml:
application.properties:
@MapperScan to your main class:
Basic CRUD
ExtendBaseMapper<T> to inherit full CRUD without writing SQL:
Conditional queries
TheQueryWrapper lets you build type-safe WHERE clauses without string concatenation:
Pagination
Register the pagination interceptor once:selectPage: