7. Spring Frameworkの概要
Spring Frameworkの仕組み
• モジュールの種類
モジュール 説明
Core IoCとDIの機能を提供する基本パッケージ
Context IoCとDIの機能をフレームワークスタイルで利用可能にする
パッケージ
DAO JDBC抽象化レイヤの提供やトランザクション管理を行うため
のパッケージ
ORM JPA、JDO、Hibernate、iBATISなどのO/Rマッピングフレーム
ワークとの連携を行うためのパッケージ
AOP AOPを利用ためのパッケージ
Web Webアプリケーションで利用可能な機能を提供するパッケージ
WebWorkやStrutsとの連携時にも利用
MVC Webアプリケーション用のMVCフレームワークを提供するパッ
ケージ
8. Spring Frameworkの概要
コンテナの起動方法
• ClassPathXmlApplicationContext
– クラスパスからファイルを読み込んで起動
ApplicationContext ac ==
ApplicationContext ac
new ClassPathXmlApplicationContext(quot;applicationContext.xmlquot;);
new ClassPathXmlApplicationContext(quot;applicationContext.xmlquot;);
• FileSystemXmlApplicationContext
– パス指定でファイルを読み込んで起動
ApplicationContext ac ==
ApplicationContext ac
new FileSystemClassPathXmlApplicationContext(quot;conf/appContext.xmlquot;);
new FileSystemClassPathXmlApplicationContext(quot;conf/appContext.xmlquot;);
22. Spring Frameworkの概要
AOP
• Spring AOPで利用可能なadvice
advice 説明
Before advice join pointの前
After returning advice join pointのメソッドが正しく実行された後
After throwing advice join pointのメソッドで例外が発生した後
After advice 結果に関係なくjoin pointの後
Around advice join pointの前後
join pointを呼び出すかどうかを制御可能。
23. Spring Frameworkの概要
AOP
• @Aspectアノテーションを利用した方法
<!-- @Aspectアノテーションを有効化 -->
<!-- @Aspectアノテーションを有効化 -->
<aop:aspectj-autoproxy/>
<aop:aspectj-autoproxy/>
<bean id=“smpl5.hogeAspect” class=“sample5.HogeAspect”/>
<bean id=“smpl5.hogeAspect” class=“sample5.HogeAspect”/>
@Aspect
@Aspect
public class HogeAspect {{
public class HogeAspect
@Pointcut(quot;execution(public **sample5.HogeImpl.*(..))quot;)
@Pointcut(quot;execution(public sample5.HogeImpl.*(..))quot;)
private void hogeOperation() {}
private void hogeOperation() {}
@Before(“hogeOperation()”) //pointcutを指定
@Before(“hogeOperation()”) //pointcutを指定
public void beforeAdvice() {{
public void beforeAdvice()
}}
@AfterReturning(“within(sample5.*)”) //join pointを直接指定
@AfterReturning(“within(sample5.*)”) //join pointを直接指定
public void afterReturingAdvice() {{
public void afterReturingAdvice()
}}
}}
48. Spring Framework2.5の新機能
アノテーションによるBean定義
• @Scope
– Beanのスコープを設定する
– Bean定義ファイルと同様の指定が可能
– scoped-proxyはBean定義ファイルで行う
@Component(quot;hogequot;)
@Component(quot;hogequot;)
@Scope(quot;prototypequot;)
@Scope(quot;prototypequot;)
public class HogeImpl implements Hoge {{
public class HogeImpl implements Hoge
}}
49. Spring Framework2.5の新機能
アノテーションによるライフサイクルメソッド
• @PostConstruct、@PreDestroy
– Bean生成時や破棄のタイミングで実行される
コールバックメソッドに指定
– JSR-250のアノテーション
public class HogeImpl implements Hoge {{
public class HogeImpl implements Hoge
@PostConstruct
@PostConstruct
public void init() {{
public void init()
}}
@PreDestroy
@PreDestroy
public void destroy() {{
public void destroy()
}}
}}