ขอบเขตของบีน
เรียนรู้ขอบเขต singleton, prototype, request และ session
ขอบเขตของบีน เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What a Scope Controls
A bean’s scope determines how many instances the container creates and how long each lives. The default and most common scope is singleton.
Choosing the right scope matters for state, memory, and thread safety.
Singleton Scope
With singleton scope, the container creates exactly one instance per ApplicationContext and returns it for every injection point. This is ideal for stateless services.
@Service // singleton by default
public class PricingService {
public BigDecimal price(Order o) { /* stateless */ }
}Singletons Must Be Thread-Safe
Because one singleton serves all concurrent requests, it must not hold mutable per-request state in fields. Keep singletons stateless, or guard shared state carefully.
- Good: read-only config, injected collaborators
- Bad: a mutable field updated per request
Prototype Scope
Prototype scope creates a new instance every time the bean is requested or injected. Use it for stateful, short-lived helpers that should not be shared.
@Component
@Scope("prototype")
public class ReportBuilder {
private final List<String> rows = new ArrayList<>();
public void addRow(String r) { rows.add(r); }
}The Prototype-in-Singleton Trap
If you inject a prototype into a singleton via the constructor, you get one instance, captured once — not a fresh one per use. The singleton is built only once, so the prototype is resolved only once.
Getting Fresh Prototypes
To obtain a new prototype each time inside a singleton, inject an ObjectProvider (or a lookup) and call it on demand.
@Service
public class ReportService {
private final ObjectProvider<ReportBuilder> builders;
public ReportService(ObjectProvider<ReportBuilder> builders) {
this.builders = builders;
}
public Report build() {
ReportBuilder rb = builders.getObject(); // fresh each call
return rb.toReport();
}
}Web Scopes Overview
In a web application, two extra scopes tie a bean’s lifetime to an HTTP interaction: request and session. They exist only while a request or session is active.
Request Scope
A request-scoped bean is created once per HTTP request and discarded when it completes. Useful for per-request context like a correlation id or accumulated request data.
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class RequestContext {
private String correlationId;
// getters/setters
}Session Scope
A session-scoped bean lives for the duration of a user’s HTTP session, holding per-user state across multiple requests, such as a shopping cart in a server-rendered app.
@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class UserCart {
private final List<Item> items = new ArrayList<>();
}Why Scoped Proxies
Injecting a short-lived scoped bean into a long-lived singleton needs a proxy. The proxy is injected once but delegates each call to the correct current-request or current-session instance.
That is why request/session beans use proxyMode = TARGET_CLASS.
Choosing a Scope
Guidelines:
- singleton — default; stateless shared services
- prototype — stateful, per-use helpers
- request — per-HTTP-request context
- session — per-user state across requests
Quick Check
Test your understanding of scopes.
Recap
Scope controls instance count and lifetime.
- singleton (default) — one per context, keep stateless
- prototype — new instance per request; beware the singleton trap
- request/session — web scopes needing scoped proxies
- Use
ObjectProviderfor fresh prototypes inside singletons
คำถามที่พบบ่อย
บทเรียน “ขอบเขตของบีน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ขอบเขตของบีน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ขอบเขตของบีน”
เรียนรู้ขอบเขต singleton, prototype, request และ session คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ขอบเขตของบีน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ