開発者ドキュメント

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

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

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

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

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

通常の豆。

package com.mkyong.customer.dao;

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

DAO層。

package com.mkyong.customer.services;

import com.mkyong.customer.dao.CustomerDAO;

public class CustomerService
{
    CustomerDAO customerDAO;

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

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

}

Springの通常のBean設定であるBean設定ファイル(Spring-Customer.xml)。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="customerService" class="com.mkyong.customer.services.CustomerService">
        <property name="customerDAO" ref="customerDAO"/>
    </bean>

    <bean id="customerDAO" class="com.mkyong.customer.dao.CustomerDAO"/>

</beans>

それを実行します

package com.mkyong.common;

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

import com.mkyong.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.mkyong.customer.dao;

import org.springframework.stereotype.Component;

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

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

package com.mkyong.customer.services;

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

import com.mkyong.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コンテナに登録します。

<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-2.5.xsd”&gt

;

<context:component-scan base-package="com.mkyong.customer"/>

</beans>

それを実行します

package com.mkyong.common;

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

import com.mkyong.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
...

Now, you can retrieve it with this name ‘AAA’.

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

===オートコンポーネントスキャンの注釈タイプ

Spring 2.5では、4つのタイプの自動コンポーネント走査アノテーション
タイプ

だから、どちらを使うの?本当に問題ではありません。ソースを見てみましょう

@ Repository

、` @Service`、 `@Controller`のコードです。

@Target({ElementType.TYPE})@ Retention(RetentionPolicy.RUNTIME)@Documented @Component public @interfaceリポジトリ{

文字列の値()既定値 "";

}

あなたは

@ Repository

、` @Service`、 `@Controller`の全てが
@コンポーネントで注釈が付けられています。ですから、@Componentだけをすべての
自動スキャンのコンポーネントですか?はい、できますし、Springは自動スキャンします
@Componentを持つすべてのコンポーネントに注釈が付けられます。

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


DAO層

パッケージcom.mkyong.customer.dao;

インポートorg.springframework.stereotype.Repository;

@リポジトリ
パブリッククラスCustomerDAO
{
    @オーバーライド
    public String toString(){
        return "こんにちは、これはCustomerDAOです";
    }
}


サービスレイヤ

パッケージcom.mkyong.customer.services;

インポートorg.springframework.beans.factory.annotation.Autowired;インポートorg.springframework.stereotype.Service;

import com.mkyong.customer.dao.CustomerDAO;

@ServiceパブリッククラスCustomerService {@Autowired CustomerDAO customerDAO;

@オーバーライド
    public String toString(){
        return "CustomerService[customerDAO =" + customerDAO + "]";
    }
        
}

===ソースコードをダウンロードする

それをダウンロードする –

Spring-Auto-Scan-Component-Example.zip


spring

モバイルバージョンを終了