0Pricing
Spring Boot 4 Microservices & REST APIs · บทเรียน

การเรียกกลับตามวงจรชีวิต

เชื่อมการทำงานเข้ากับขั้นตอนเริ่มต้นและทำลายบีน

การเรียกกลับตามวงจรชีวิต เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Bean Lifecycle

Beyond construction, beans pass through phases: the container instantiates them, injects dependencies, runs initialization callbacks, serves requests, then runs destruction callbacks at shutdown.

Hooking into these phases lets you set up and clean up resources reliably.

Why Not Use the Constructor?

A constructor runs before dependencies are injected when using setter/field injection, so collaborators may still be null. Initialization that depends on injected beans belongs in a post-construction callback, not the constructor.

With constructor injection the dependencies are present, but lifecycle callbacks still give a clear, intent-revealing place for setup.

@PostConstruct

A method annotated @PostConstruct runs once after the bean is fully constructed and all dependencies are injected. Use it for warm-up tasks like priming a cache or validating state.

@Service
public class CatalogService {
    private final ProductRepository repo;
    private List<Product> cache;
    public CatalogService(ProductRepository repo) { this.repo = repo; }

    @PostConstruct
    void warmUp() {
        this.cache = repo.findAllActive();
    }
}

@PreDestroy

A method annotated @PreDestroy runs once when the context is shutting down, before the bean is destroyed. Use it to release resources like connections, thread pools, or file handles.

@Service
public class ConnectionManager {
    private final ExecutorService pool = Executors.newFixedThreadPool(4);

    @PreDestroy
    void shutdown() {
        pool.shutdown();
    }
}

The InitializingBean Interface

Implementing InitializingBean provides afterPropertiesSet, called after dependencies are set. It works but couples your class to Spring’s API, so annotations are usually preferred.

@Service
public class IndexService implements InitializingBean {
    @Override
    public void afterPropertiesSet() {
        buildIndex();
    }
}

The DisposableBean Interface

Symmetrically, DisposableBean supplies a destroy method for cleanup. Like InitializingBean, it ties you to Spring interfaces.

@Service
public class IndexService implements DisposableBean {
    @Override
    public void destroy() {
        releaseIndex();
    }
}

@Bean initMethod and destroyMethod

For beans you create with @Bean (often third-party types), specify lifecycle methods by name. This keeps the external class free of Spring annotations.

@Bean(initMethod = "start", destroyMethod = "stop")
public CacheClient cacheClient() {
    return new CacheClient(config);
}

Ordering of Callbacks

When several mechanisms are present, the init order is: @PostConstruct first, then InitializingBean.afterPropertiesSet, then a custom initMethod. Destruction mirrors this with @PreDestroy first.

Preferred Approach

For modern code, prefer the JSR-250 annotations @PostConstruct and @PreDestroy. They are concise, framework-neutral, and clearly express intent without coupling to Spring interfaces.

A Caveat on Prototype Destruction

The container does not call destruction callbacks for prototype beans — it hands them off and forgets them. If a prototype holds resources, you are responsible for releasing them yourself.

Graceful Shutdown

Spring Boot triggers destruction callbacks during a clean shutdown (for example on SIGTERM). Combined with graceful shutdown settings, this lets in-flight work finish and resources close cleanly before exit.

server:
  shutdown: graceful
spring:
  lifecycle:
    timeout-per-shutdown-phase: 20s

Quick Check

Test your understanding of lifecycle callbacks.

Recap

Lifecycle callbacks manage setup and cleanup.

  • @PostConstruct runs after dependencies are injected
  • @PreDestroy runs before shutdown for cleanup
  • InitializingBean/DisposableBean work but couple to Spring
  • Use initMethod/destroyMethod for @Bean-created types
  • Prototype destruction is your responsibility

คำถามที่พบบ่อย

บทเรียน “การเรียกกลับตามวงจรชีวิต” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเรียกกลับตามวงจรชีวิต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเรียกกลับตามวงจรชีวิต”

เชื่อมการทำงานเข้ากับขั้นตอนเริ่มต้นและทำลายบีน คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การเรียกกลับตามวงจรชีวิต” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม

ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. คอนเทนเนอร์ Spring IoC
  2. การฉีดผ่านคอนสตรักเตอร์เทียบกับฟิลด์
  3. ขอบเขตของบีน
  4. การเรียกกลับตามวงจรชีวิต
← กลับไปที่ Spring Boot 4 Microservices & REST APIs