[学習記録] Spring
内容:2章 SpringのDI
DIとは何か
DIの使い所
アノテーションを使ったDI
- @Autowiredと@Component
@Component public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; ... }
- Bean定義ファイル
<?xml version="1.0" encoding="UTF-8"?> <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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:annotation-config /> <context:component-scan base-package="sample.*"/> </beans>
- 主なスキーマ
名称 | 説明 |
bean | Bean(コンポーネント)の設定 |
context | Beanの検索やアノテーションの設定 |
util | 定数定義やプロパティファイルの読み込みなどのutility機能の設定 |
jee | JNDIおよびEJBのlookupの設定 |
lang | スクリプト言語を利用する場合の設定 |
aop | AOPの設定 |
tx | トランザクションの設定 |
mvc | SpringMVCの設定 |
@Autowiredと@Component
- @Autowired
@Autowired public void setFoo(Foo foo) { this.foo = fop; } @Autowired public void setFooBar(Foo foo, Bar bar) { this.foo = foo; this.bar = bar; }
- コンポーネントごとにスキャンして必要なとき(テスト時など)に書き換え
<context:component-scan base-package="sample.di.business.*" /> <context:component-scan base-package="sample.di.dao.*" />
アノテーション | 解説 |
@Controller | プレゼンテーション層Spring MVC用アノテーション。 |
@Service | ビジネスロジック層Service用アノテーション。トランザクション管理できるという噂もあったが・・・ |
@Repository | データアクセス層DAO用アノテーション。例外を全てDataAccessExceptionに変換 |
- @Scope
- @Lazy
Bean定義ファイルでDI
- BeanFactory
- Bean定義ファイルの書き方は以下。
<beans> <!-- クラスYにXをAutowiredでインジェクションする --> <bean id="Xのオブジェクト名" class="Xのパッケージ名.Xのクラス名" /> <bean id="Yのオブジェクト名" class="Yのパッケージ名.Yのクラス名" autowire="byType" /> <!-- クラスYにXを明示的にインジェクションする --> <bean id="Xのオブジェクト名" class="Xのパッケージ名.Xのクラス名" /> <bean id="Yのオブジェクト名" class="Yのパッケージ名.Yのクラス名" > <property name="インスタンス変数名" ref="Xのオブジェクト名" /> <!-- 例 --> <bean id="productService" class="sample.di.business.service.ProductServiceImpl" autowire="byType" /> <bean id="productDao" class="sample.di.dataaccess.ProductDaoImpl" /> </beans>
- beanタグの属性
属性 | 意味 |
id | オブジェクトを一意に示すID |
name | オブジェクト名を定義(オブジェクトに複数の名前をつけたりする場合に使用) |
class | idの実態。パッケージ名とクラス名から成る |
parent | 設定を引き継ぐBeanのクラス名を指定 |
abstract | true/false、trueでインスタンスを生成せずに共通の設定を定義したい場合。省略時false |
singleton | true/false、trueでgetBeanメソッドで取得するインスタンスはシングルトンに。省略時true |
lazy-init | true/false。trueでインスタンス化を遅らせる。省略時false |
autowire | no/byName/byType/constructor/autodetect。省略時no |
dependency-check | none/simple/object/all。インスタンス変数へのインジェクションのチェックをするか否か。省略時none |
depend-on | 依存関係の対象となるオブジェクトの存在をチェックする。 |
init-method | メソッド名を記述することで、インスタンス変数の設定後に呼ばれる。メソッドは引数がないことが条件。 |
destroy-method | システムh数量じに呼ばれるメソッド。 |
- プロパティファイルの利用
- メッセージなどのプロパティファイルを読み込みたい場合
- util:propertiesを使用
<util:properties id="msgProperties" location="classpath:sample/config/message.properties" /> <bean id="message" class="sample.MessageServiceImpl"> <property name="message" value="#{msgProperties.message}" /> </bean>