春自動スキャンコンポーネント

Spring Autoスキャンコンポーネント

通常、SpringコンテナがBeanまたはコンポーネントを検出して登録できるように、XML Bean構成ファイルですべてのBeanまたはコンポーネントを宣言します。 実際、Springは、事前に定義されたプロジェクトパッケージからBeanを自動スキャン、検出、およびインスタンス化することができ、XMLファイルでの面倒なBean宣言は不要です。

以下は、カスタマーサービスとdaoレイヤーを含む単純なSpringプロジェクトです。 コンポーネントの手動宣言とSpringでの自動コンポーネントスキャンの違いを調べてみましょう。

1. コンポーネントを手動で宣言します

SpringでBeanを宣言する通常の方法を参照してください。

通常の豆。

package com.example.customer.dao;

public class CustomerDAO
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }
}

DAOレイヤー。

package com.example.customer.services;

import com.example.customer.dao.CustomerDAO;

public class CustomerService
{
    CustomerDAO customerDAO;

    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }

}

Bean構成ファイル(Spring-Customer.xml)、Springの通常のBean構成。



    
        
    

    

それを実行します

package com.example.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.example.customer.services.CustomerService;

public class App
{
    public static void main( String[] args )
    {
        ApplicationContext context =
          new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});

        CustomerService cust = (CustomerService)context.getBean("customerService");
        System.out.println(cust);

    }
}

出力

CustomerService [customerDAO=Hello , This is CustomerDAO]

2. 自動コンポーネントスキャン

次に、Spring自動コンポーネントスキャン機能を有効にします。

これがクラスが自動スキャンコンポーネントであることを示すために、@Componentで注釈を付けます。

package com.example.customer.dao;

import org.springframework.stereotype.Component;

@Component
public class CustomerDAO
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }
}

DAOレイヤー、@Componentを追加して、これも自動スキャンコンポーネントであることを示します。

package com.example.customer.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.customer.dao.CustomerDAO;

@Component
public class CustomerService
{
    @Autowired
    CustomerDAO customerDAO;

    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
}

この「context:component」をBean構成ファイルに入れます。つまり、Springで自動スキャン機能を有効にします。 base-packageは、コンポーネントが保存されている場所を示します。Springはこのフォルダーをスキャンし、Bean(@Componentアノテーションが付けられている)を見つけて、Springコンテナーに登録します。



    

それを実行します

package com.example.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.example.customer.services.CustomerService;

public class App
{
    public static void main( String[] args )
    {
        ApplicationContext context =
           new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});

        CustomerService cust = (CustomerService)context.getBean("customerService");
        System.out.println(cust);

    }
}

出力

CustomerService [customerDAO=Hello , This is CustomerDAO]

これがSpringでの自動コンポーネントスキャンの仕組みです。

カスタム自動スキャンコンポーネント名

デフォルトでは、Springはコンポーネントの最初の文字を「CustomerService」から「customerService」まで小文字にします。 そして、「customerService」という名前でこのコンポーネントを取得できます。

    CustomerService cust = (CustomerService)context.getBean("customerService");

コンポーネントのカスタム名を作成するには、次のようなカスタム名を入力できます。

@Service("AAA")
public class CustomerService
...

これで、この名前「AAA」で取得できます。

    CustomerService cust = (CustomerService)context.getBean("AAA");

自動コンポーネントスキャンの注釈タイプ

Spring 2.5では、4種類の自動コンポーネントスキャン注釈タイプがあります

  • @Component –自動スキャンコンポーネントを示します。

  • @Repository –永続層のDAOコンポーネントを示します。

  • @Service –ビジネスレイヤーのサービスコンポーネントを示します。

  • @Controller –プレゼンテーションレイヤーのコントローラーコンポーネントを示します。

それで、どちらを使用するのですか? 本当に関係ありません。 @Repository@Service、または@Controllerのソースコードを見てみましょう。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {

    String value() default "";

}

すべての@Repository@Service、または@Controller@Componentの注釈が付けられていることに気付くでしょう。 それで、自動スキャンのすべてのコンポーネントに@Componentだけを使用できますか? はい、できます。Springはすべてのコンポーネントを@Componentアノテーション付きで自動スキャンします。

読みやすくするためにはうまくいきますが、良い習慣ではありません。次のように、コードを読みやすくするために、指定されたレイヤーの@ Repository、@ Serviceまたは@Controllerを常に宣言する必要があります。

DAO層

package com.example.customer.dao;

import org.springframework.stereotype.Repository;

@Repository
public class CustomerDAO
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }
}

サービス層

package com.example.customer.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.customer.dao.CustomerDAO;

@Service
public class CustomerService
{
    @Autowired
    CustomerDAO customerDAO;

    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }

}

ソースコードをダウンロード