Java Backend Guide: @Autowired vs @Resource, MySQL Engines, Redis, JVM OOM
The article shares ZTE salary offers and interview tips, then dives into technical deep‑dives on Spring bean injection annotations, compares MySQL InnoDB and MyISAM, explains Redis usage patterns, and walks through JVM OutOfMemoryError diagnosis with concrete code and tooling examples.
The author begins by announcing ZTE's recent campus recruitment results, listing salary ranges for various software development positions (e.g., 15‑18k·12 in Xi'an, 16‑19k·12 in Nanjing, 19‑22k·12 in Shanghai, 25‑28k·12 for "Future Leader" roles in Shenzhen) and noting that many offers target 985 master graduates who often have better alternatives.
Interview Experience
For Java "Future Leader" interviews, the author advises preparing a concise self‑introduction, highlighting internship contributions, and, if necessary, embellishing modest tasks into clear achievements. Sample responsibilities include core order‑module development, blacklist dashboard creation, and a Redisson‑based rate‑limiting component.
Spring Bean Injection Annotations
Spring provides @Autowired (type‑first injection) while JDK offers @Resource (name‑first) and @Inject. @Autowired first matches by type; if multiple beans match, it falls back to bean name. @Resource prefers name matching, then type. When multiple implementations exist, explicit naming via @Qualifier (for @Autowired) or the name attribute (for @Resource) resolves ambiguity. The author recommends @Resource for name‑centric scenarios and @Autowired with constructor injection for immutable dependencies.
// Example: two SmsService implementations
@Autowired
private SmsService smsService; // fails: ambiguous
@Autowired
private SmsService smsServiceImpl1; // works: name matches
@Autowired
@Qualifier("smsServiceImpl1")
private SmsService smsService; // works: explicit qualifierMySQL Storage Engine Comparison
MyISAM (pre‑5.5 default) offers table‑level locking, no transactions, no foreign keys, no MVCC, and cannot recover from crashes. InnoDB (default since 5.5) provides row‑level locking, full ACID transactions with four isolation levels (default REPEATABLE‑READ), foreign‑key support, MVCC, crash‑safe redo‑log recovery, and generally superior performance, especially under concurrent writes.
Why Indexes Are Fast
Indexes use a B+Tree structure that is shallow (typically 3‑4 levels for millions of rows), reducing disk I/O to a few reads. Leaf nodes are linked, enabling sequential reads that benefit from prefetching.
Analyzing SQL with EXPLAIN
Running EXPLAIN SELECT ... shows the execution plan without executing the query. Important columns include type (access method), possible_keys , key (actual index used), rows (estimated rows examined), and Extra (additional info such as "Using filesort").
Redis Usage in Projects
Distributed Lock : typically implemented with Redisson.
Rate Limiting : Redis + Lua scripts or Redisson's RRateLimiter (token‑bucket).
Message Queue : List for simple queues; Stream (Redis 5.0) for Kafka‑like features.
Delayed Queue : Redisson's sorted‑set based delayed queue.
Distributed Session : store session data in String or Hash.
Complex Scenarios : bitmap for active‑user stats, sorted set for leaderboards.
Cache‑Aside Pattern
Write flow: update DB first, then delete the cache entry. Read flow: try cache; on miss, read DB, then populate cache. Deleting the cache avoids costly recomputation and reduces inconsistency risk in high‑concurrency updates. If cache deletion fails, shorten TTL or implement retry (often via a message queue).
JVM Memory Areas and OOM
The JVM runtime data areas include the program counter (never OOM), Java heap, young/old generations, metaspace, native memory, thread stacks, and native method stacks. All except the program counter can trigger OutOfMemoryError.
Diagnosing OOM
Tools such as MAT or JVisualVM analyze heap dumps. MAT’s "Leak Suspects" report highlights dominant objects; the Dominator Tree shows retention relationships. The author provides a reproducible leak example where a static List<byte[]> continuously grows, leading to a heap‑space OOM. The heap dump reveals the SimpleLeak class and its Object[] as the primary memory consumer.
public class SimpleLeak {
public static List<byte[]> staticList = new ArrayList<>();
public void leakMethod() {
staticList.add(new byte[1024 * 1024]); // 1 MB
}
public static void main(String[] args) throws InterruptedException {
SimpleLeak leak = new SimpleLeak();
for (int i = 0; i < 200; i++) {
leak.leakMethod();
Thread.sleep(200);
}
Thread.sleep(Long.MAX_VALUE);
}
}Running with
-Xmx128m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=simple_leak.hproftriggers an OOM after ~115 MB allocation, and MAT identifies the static list as the leak root.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaGuide
Backend tech guide and AI engineering practice covering fundamentals, databases, distributed systems, high concurrency, system design, plus AI agents and large-model engineering.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
