この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:5
ページ更新者:T
更新日時:2019-02-02 04:41:23

タイトル: @Component
SEOタイトル: 【Spring】@Componentアノテーションとは

この記事の要点
  • @Component はクラスをSpring の DI 対象として登録するアノテーション
  • 付与したクラスは ApplicationContext に Bean として入り、@Autowired で注入できる
  • コンポーネントスキャン(@ComponentScan 配下)の対象になる
  • @Service / @Repository / @Controller はすべて @Component の特化版

 

本稿はSpringFrameworkの@Componentについて説明します。

@Componentとは

@Component付与したクラスはSpirngのコンポーネントとして認識され、ApplicationContextに登録されることで、DI対象のクラスとなります。(@Autowiredで指定できるようになる)

以下、@Componentの簡単なサンプルです。

@Component
public class TestComponent {

    public void componentMethod() {
        System.out.println("This is Component!");
    }

}