Springの年譜
• 2002年11月 書籍:Expert One-on-One J2EE
• 2004年3月 Spring 1.0
• 2004年9月 Spring 1.1
• 2005年5月 Spring 1.2
• 2006年10月 Spring 2.0
• 2007年11月 Spring2.5
• 2009年12月 Spring3.0
• 2011年12月 Spring3.1
‒ Java7, Spring Cache...
• 201x年 Spring3.2
‒ Hibernate4 FullSupport...
5
6.
Springの主なプロジェクト
• Spring AMQP • Spring Mobile
• Spring Android • Spring .NET
• Spring Batch • Spring Roo
• Spring BlazeDS • Spring Security
• Spring Data • Spring Security
• Spring Framework OAuth
• Spring Gemfire • Spring Social
• Spring Integration • Spring Web Flow
• Spring Web
Services
6
インタフェース導入の難しさ
• インタフェースの実装を、インタフェースを利用するク
ラスが生成(インスタンス化)してはいけない
public class R { ②利用
・・・ QImpl
Q q = new QImpl(); // ダメ R (Qの実装)
q.methodA(); Q
String m
= q.methodB();
・・・ ①生成
27
28.
インタフェース導入とFactory
• インタフェースの実装を生成(インスタンス化)する
Factory(FactoryMethod)を用意(自作)すること
で解決する
public class R {
③利用
・・・
QImpl
Q q =
R (Qの実装)
(Q)Factory.create(KEY);
Q
①生成の
q.methodA();
要求
String m
②生成
Factory
= q.methodB();
・・・
KEY=QImpl
28
アノテーションを利用したDI
• インジェクションの設定(自動検出)
‒ @Autowired
• Beanの設定
‒ @Component
Injection
class Service { @Component
@Autowired class DaoImpl
private Dao dao; implements Dao {
... ...
} Dao }
37
問題 2つあったら?
• Autowireでインタフェースを実装するク
ラスが2つあったらどうなる?
class Service { @Component
@Autowired class ADaoImpl
private Dao dao; implements Dao {
... ...
} Dao }
@Component
class BDaoImpl
implements Dao {
...
}
45.
問題 じゃあ、Unitテストどうする?
class Service{ @Component
@Autowired class DaoImpl
private Dao dao; implements Dao {
... ...
} Dao }
@Component
class TestDaoImpl
implements Dao {
...
}
Bean定義ファイル
<?xml version="1.0" encoding="UTF-8"?> contextがないことに注意
<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-3.0.xsd">
<bean id = "service"
class = "sample.di.service. ServiceImpl">
<property name = "dao" ref = "dao"/>
</bean>
<bean id = "dao"
class = "sample.di.dataaccess.DaoImpl" />
</beans>
55
56.
主要タグの主な属性
<bean>
属性
意味
id システム内で一意とするオブジェクト名。
name Bean定義ファイル内で、id以外に名前を設定したい場合に設
定する別名。
空白、「,」、「;」で区切ることにより、複数の値を設定できる。
class idの実態。パッケージ名+クラス名。
<property>
属性
意味
name ref属性で指定したオブジェクトをインジェクションする属性の名
前。「set + “name属性で指定した属性名”」のメソッドを利用
する(属性名は使われないことに注意)。
ref インジェクションしたいbeanのid(beanタグのid属性で指定され
た名前)。
56
57.
セッタインジェクション
ServiceImpl DaoImpl
Dao
setDao
Spring
public class ServiceImpl implements Service {
private Dao dao;
public void setDao(Dao dao) {
this. dao = dao;
}
…
57
58.
定義ファイル
ServiceImpl DaoImpl
Dao
setDao
Spring
・・・
<bean id = service"
class = com.starlight.business.ServiceImpl">
<property name = dao ref = dao"/>
</property>
</bean>
<bean id = dao"
class = com.starlight.dataaccess.DaoImpl" />
・・・
58
59.
定義ファイル
ServiceImpl DaoImpl
Dao
setDao
Spring
・・・
<bean id = service"
class = com.starlight.business.ServiceImpl" autowire="byType" />
<bean id = dao"
class = com.starlight.dataaccess.DaoImpl" />
・・・
59
60.
問題 じゃあ、これはどうなる?
• クラス図を書いてみてください
・・・
<bean id="hogeService"
class="sample.HogeServiceImpl">
<property name = "hogeDao" ref = "hogeDao"/>
<property name = "hogehogeDao" ref = "hogehogeDao"/>
</bean>
<bean id = "hogeDao" class = "sample.HogeDaoImpl" />
<bean id = "hogehogeDao" class = "sample.HogehogeDaoImpl" />
・・・