Spring Boot 4 Complete Guide · レッスン

Beanスコープとライフサイクル管理

SpringがBeanを生成、スコープ管理、破棄する仕組みと、コールバックでそのライフサイクルにフックする方法を学びます。

レッスン 4/413 ステップ

「Beanスコープとライフサイクル管理」はCoddyKit上の無料Spring Boot 4 Complete Guideレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Complete Guide学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Complete Guideコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What Is a Bean Scope?

A scope defines how many instances of a bean Spring creates and how long they live. The default scope is singleton: one shared instance per container.

Singleton Scope

With singleton, every injection point receives the same object. This is ideal for stateless services and is the most common choice.

@Service
public class PricingService {
    // one shared instance across the app
}

Prototype Scope

The prototype scope creates a new instance every time the bean is requested. Use it for stateful or short-lived objects.

@Component
@Scope("prototype")
public class ReportBuilder {
}

Web-Aware Scopes

In web apps you also have request and session scopes, tying a bean's lifetime to a single HTTP request or a user session respectively.

  • request — one per HTTP request
  • session — one per user session

Choosing the Right Scope

Default to singleton for stateless logic. Reach for other scopes only when a bean must hold per-request or per-instance state, since wider sharing of mutable state causes bugs.

The Bean Lifecycle

Spring instantiates a bean, injects its dependencies, runs initialization callbacks, serves it during the app's life, and finally runs destruction callbacks on shutdown.

Initialization with @PostConstruct

Annotate a method with @PostConstruct to run setup logic after dependencies are injected, such as opening a connection or warming a cache.

@PostConstruct
public void init() {
    System.out.println("Bean is ready");
}

Cleanup with @PreDestroy

A @PreDestroy method runs just before the bean is removed, letting you release resources cleanly.

@PreDestroy
public void cleanup() {
    System.out.println("Releasing resources");
}

Lazy Initialization

By default singletons are created at startup. Marking a bean @Lazy defers creation until it is first needed, which can speed up boot time for rarely used beans.

@Lazy
@Service
public class HeavyService {
}

Prototype Beans and Destruction

Be aware that Spring does not manage the full destruction lifecycle of prototype beans. The container hands them off and forgets them, so you must release their resources yourself.

Putting It Together

A typical singleton service uses @PostConstruct to initialize and @PreDestroy to clean up, giving you precise control over startup and shutdown behavior.

@Service
public class CacheService {
    @PostConstruct void load() {}
    @PreDestroy void flush() {}
}

Quick Check

Test your understanding of bean scopes and lifecycle.

Recap

You learned the singleton, prototype, request, and session scopes, the bean lifecycle, and how to hook into it with @PostConstruct, @PreDestroy, and @Lazy.

無料で開始

AI チューターと学ぶ Java — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
21
レッスン
84

よくある質問

「Beanスコープとライフサイクル管理」レッスンは無料ですか?

はい。「Beanスコープとライフサイクル管理」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Complete Guideコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Complete Guideコースには全4レッスンが含まれています。

「Beanスコープとライフサイクル管理」で何を学びますか?

SpringがBeanを生成、スコープ管理、破棄する仕組みと、コールバックでそのライフサイクルにフックする方法を学びます。 ブラウザで直接実行するハンズオンコードでSpring Boot 4 Complete Guideを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Spring Boot 4 Complete Guideを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSpring Boot 4 Complete Guideは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「Beanスコープとライフサイクル管理」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSpring Boot 4 Complete Guideレッスンでコードを書いて実行できますか?

はい。すべてのSpring Boot 4 Complete Guideレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Spring IoCコンテナの基礎
  2. 依存性注入の実践
  3. アプリケーションプロパティの外部化
  4. Beanスコープとライフサイクル管理
← Spring Boot 4 Complete Guideに戻る