Spring Beanとは何ですか?

Spring Beanとは何ですか?

1. 概要

Beanは、Spring Frameworkの重要な概念です。 そのため、この概念を理解することは、フレームワークを理解し、効果的に使用するために重要です。

残念ながら、there aren’t clear answers to a simple question – what a Spring bean really is.の説明の中には、全体像を見逃してしまうほど低いレベルのものもあれば、曖昧すぎるものもあります。

このチュートリアルでは、公式ドキュメントの説明から始めて、トピックに光を当てようとします。

参考文献:

JavaフレームワークとしてSpringを選択する理由

Springフレームワークの主な価値提案の迅速かつ実用的な概要。

SpringでのgetBean()の理解

SpringコンテナからBeanインスタンスを取得するためのSpringのBeanFactory.getBean()メソッドのさまざまなバリアントについて学習します

2. Beanの定義

the Spring Framework documentationでのBeanの定義は次のとおりです。

Springでは、アプリケーションのバックボーンを形成し、SpringIoCコンテナによって管理されるオブジェクトはBeanと呼ばれます。 Beanは、Spring IoCコンテナーによってインスタンス化、アセンブル、または管理されるオブジェクトです。

この定義は簡潔で要点に到達します。but misses an important thing – Spring IoC container.うさぎの穴を下って、それが何であるか、そしてそれがもたらす利点を見てみましょう。

3. 制御の反転

簡単に言えば、Ihttps://www.example.com/inversion-control-and-dependency-injection-in-spring [nversion of Control]、または略してIoCはa process in which an object defines its dependencies without creating them.ですこのオブジェクトはデリゲートしますIoCコンテナへのそのような依存関係を構築する仕事。

IoCに飛び込む前に、いくつかのドメインクラスの宣言から始めましょう。

3.1. ドメインクラス

クラス宣言があると仮定します。

public class Company {
    private Address address;

    public Company(Address address) {
        this.address = address;
    }

    // getter, setter and other properties
}

このクラスには、タイプAddressの共同編集者が必要です。

public class Address {
    private String street;
    private int number;

    public Address(String street, int number) {
        this.street = street;
        this.number = number;
    }

    // getters and setters
}

3.2. 従来のアプローチ

通常、クラスのコンストラクターでオブジェクトを作成します。

Address address = new Address("High Street", 1000);
Company company = new Company(address);

このアプローチには何の問題もありませんが、依存関係をより適切に管理するのは良いことではないでしょうか。

数十または数百のクラスを持つアプリケーションを想像してください。 クラスの単一インスタンスをアプリケーション全体で共有したい場合もあれば、ユースケースごとに個別のオブジェクトが必要な場合などもあります。

このような多数のオブジェクトを管理することは、悪夢にほかなりません。 This is where Inversion of Control comes to the rescue.

オブジェクトは、それ自体で依存関係を構築する代わりに、IoCコンテナーから依存関係を取得できます。 All we need to do is to provide the container with appropriate configuration metadata.

3.3. Beanの構成

まず、Companyクラスを@Componentアノテーションで装飾しましょう。

@Component
public class Company {
    // this body is the same as before
}

BeanメタデータをIoCコンテナに提供する構成クラスは次のとおりです。

@Configuration
@ComponentScan(basePackageClasses = Company.class)
public class Config {
    @Bean
    public Address getAddress() {
        return new Address("High Street", 1000);
    }
}

構成クラスは、タイプAddressのBeanを生成します。 また、Companyクラスを含むパッケージ内のBeanを検索するようにコンテナーに指示する@ComponentScanアノテーションも含まれています。

Spring IoCコンテナーがこれらのタイプのオブジェクトを構築する場合、すべてのオブジェクトはIoCコンテナーによって管理されるため、SpringBeanと呼ばれます。

3.4. IoCの動作

構成クラスでBeanを定義したため、we’ll need an instance of the AnnotationConfigApplicationContext class to build up a container:

ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

簡単なテストでは、Beanの存在とプロパティ値を検証します。

Company company = context.getBean("company", Company.class);
assertEquals("High Street", company.getAddress().getStreet());
assertEquals(1000, company.getAddress().getNumber());

この結果は、IoCコンテナーがBeanを正しく作成および初期化したことを証明しています。

4. 結論

このチュートリアルでは、Spring BeanとIoCコンテナとの関係について簡単に説明しました。

このチュートリアルの完全なソースコードは、over on GitHubにあります。