JavaのWebアプリケーション開発フレームワークによる、Webサイト開発の顛末記です。

EclipseのMavenを使った、Spring-MVC、Thymeleaf、MyBatis 等のプログラミングテクニックを、
備忘録的に記録しています。実際に動くソースコードを多用して説明していますので、
これからEclipseや、Spring-MVCを始めたいと思っている人にとって、少しでも参考になれば幸いです。
Spring-MVCの散歩道 > Spring Boot の小径 > 第4歩 Spring Boot 匍匐前進 > コンポーネントスキャン


package jp.dip.arimodoki.cntl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import jp.dip.arimodoki.blogic.BlHelloIf;
import jp.dip.arimodoki.model.FormHelloIf;

/**
 * 異なるパッケージ階層のクラスをコンポーネントスキャンするサンプル
 * @ComponentScanアノテーションでパッケージを指定する
 */
@Controller
@ComponentScan("jp.dip.arimodoki.*")        //ワイルドカードが使える
//@ComponentScan("jp.dip.arimodoki.blogic, jp.dip.arimodoki.model")  //カンマ区切りで指定する
public class HelloCntl {
    //Form Bean のDI
    @Autowired
    FormHelloIf        formHello;

    //ビジネスロジックのDI
    @Autowired
    BlHelloIf        blHello;

    /**
     * データモデルへのバインド
     */
    @ModelAttribute("FormHello")
    public FormHelloIf setUpBindObject() {
        //FormBeanをビジネスロジックに継承。
        this.blHello.setForm(this.formHello);

        return this.formHello;
    }

    @RequestMapping(value = "/hellocntl")  //URLは、http://localhost:8080/hellocntlとする
    public String hellocntl() {

        blHello.setDiscription();        //formHello.discriptionのセット

        return "hellocntl";   //src/main/resources/templates/hellocntl.html を呼び出す
    }

}