LoginSignup
2

More than 5 years have passed since last update.

SpringのDIについて②

Posted at

この記事の目的

SpringのDIについて①
の続きです

この記事の範囲

Configurationについてを主に扱う

Configurationとは?

Bean定義ファイル

機能

  • DIコンテナに対し、【どのインスタンス】【どの初期値】で渡すかを定義してくれる、BeanとDIコンテナの橋渡し役になる
  • ORMにおけるマッピングファイルに近いかもしれない
  • DIの基本的な機能である【オブジェクトの情報を外部に持つ】というものの【外部】そのものと言える
  • オブジェクトの状態をソースコード中にバラバラに置くのではなく、こういったファイルにまとめて置くことでメンテナンスが簡単になる

Configurationの概略.jpg

種類

  • Javaのクラスで持つか、XMLファイルで持つか、またそれら二つにアノテーションを組み合わせたものの4種類がある

JavaベースConfiguration

JavaConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HogeConfig {
    @Bean
    HogeService hogeService() {
        return new HogeService():
    }

    @Bean
    AAABean aAABean() {
        return new AAABean(hogeService());
    }

    @Bean(name = "bBean")
    BBBBean bBBBean() {
        return new bBBBean();
    }
}
  • Configurationクラスには@Configurationアノテーションを明示的に付与する
  • 複数アノテーションを付与することもできる
  • 各メソッドに引数を追加することで、他のBeanを参照できる
  • 明示的にBean名を定義することもできる(name = "hoge")DIコンテナからインスタンスをgetBeanする際に試用する

XMLベースConfiguration

xmlConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="hogeService" class="jp.co.hogeProgram.HogeService" />

    <bean id="aAABean" class="jp.co.hogeProgram.AAABean">
        <constructor-arg ref="hogeService"/>
    </bean>

    <bean id="bBean" class="jp.co.hogeProgram.BBBBean>
        <constructor-arg value="aaa"/>
    </bean>
</beans>
  • bean要素内でBeanを定義する
  • constoructor-argで他のBeanやスカラ値を渡すこともできる

以上二つはそれぞれBeanの数だけ定義を書かなくてはいけない為、手間がかかるため、次のアノテーションベースを組み合わせてDIを行うのが一般的である

アノテーションベースConfiguration

Bean

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

@Component
public class Hoge {

    オートワイヤリング
    このアノテーションの記述によりFugaServiceをDIコンテナから自動的にBeanを取ってくる
    @Autowired
    public Hoge(FugaService fuga);

    処理を記述
}

Configクラス(xmlは省略)
この場合はjp.co.hoge以下のクラスを探す

AppCongig.java
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("jp.co.hoge")
public class AppConfig {

}
  • 「コンポーネントスキャン」により@Componentアノテーションが付与されたクラスを自動的にDIコンテナに設定することが出来る。これを「オートワイヤリング」という

  • アノテーションは@Componentだけでなく、@Serviceなどクラスの役割によって複数存在するが、基本的に@Componentと同様である

  • コンポーネントスキャンを有効にするには、ConfigクラスorXMLに@ComponentScan("パッケージ")、もしくはcontext:component-scan要素にパッケージを記載するとパッケージ配下に存在するアノテーションを付与されているクラスを探す

  • スキャン範囲のパッケージが広い程時間がかかるので、適切な範囲を設定する必要がある

次回予告

Beanの紐づけについてやる予定です

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2