More Related Content Similar to Jjug springセッション Similar to Jjug springセッション (20) More from Yuichi Hasegawa More from Yuichi Hasegawa (16) Jjug springセッション2. 自己紹介
• 長谷川 裕一(日本Springユーザ会 会長)
‒ 1986年、イリノイ州警察指紋システムのアセンブリ言語プログ
ラマからスタートして、PL,PMと経験し、アーキテクト、コ
ンサルタントへ
‒ 現在はオブジェクト指向(特にJava/Spring)を中心に、コン
サルティング(IT戦略、技術導入/検証、プロセスetc)や教育
で活動
• 書籍
‒ プログラムの育てかた(ソフトバンク),Spring入門(技術評
論社),Spring2.0入門(技術評論社),間違いだらけのソフ
トウェア・アーキテクチャ(技術評論社)
• その他
‒ Starlight&Storm LLC代表社員、SQuBOK策定メンバ
‒ 株式会社フルネス取締役、チェンジビジョン・コンサルティン
グ・パートナー
2
3. おしながき
• Spring概要∼SpringFrameworkからSpringへ!?
‒ SpringSource.org
‒ SpringSource.com
• Spring3.1∼主な追加機能
‒ Bean定義のプロファイル機能
‒ 組込みデータベースサポート
‒ Object/XMLマッピング連携
‒ Cache Abstraction機能
‒ 非同期実行/スケジューリング実行
• これからのJava EnterpriseとSpring∼みんなの生き残り戦略
‒ Spring 現状の問題と解決の術
‒ Javaの方向性と生き残りの術(JavaOneから見えてきた)
3
6. Spring
• Spring Framework
‒ ロッド・ジョンソン氏(SpringSource[旧社名:
Interface21])が中心に開発
‒ Java/JEEアプリケーション開発用フレームワーク
• DIxAOPコンテナ
‒ ASL(Apache Software Licence) Ver2.0に基づいたオープ
ンソース
6
7. 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, Hibernate4...
• 201x年 Spring3.2
‒ Hibernate4 FullSupport...
7
8. Springの主なプロジェクト
• Spring Framework • Spring Mobile
• Spring AMQP • Spring .NET
• Spring Android • Spring Roo
• Spring Batch • Spring Security
• Spring BlazeDS • Spring Security
• Spring Data OAuth
• Spring Gemfire • Spring Social
• Spring Integration • Spring Web Flow
• Spring Web
Services
8
10. Spring 開発-実行-管理
• 開発(Java Leadership)
‒ Spring
‒ Groovy&Grails
‒ SpringSource Tool Suite
‒ Cloud Foundary
• 実行(Application Platform)
‒ vFablic ERS
‒ vFablic tc Server
‒ vFablic RabitMQ
‒ vFablic GemFire
• 管理(Apllication Management)
‒ vFablic Hyperic
14. vFabric
• VMwareのソリューション
‒ クラウド向けのアプリケーション基盤(ミドルウェア)
‒ vSphere のメモリ管理機能を利用したコンピュータリソースの
有効活用
‒ vCenter によるアプリケーションの監視
‒ 仮想マシンの数に応じた価格体系を提供
Spring + Application
vFabric
VMware
15. vFabric
• VMWare が提供するクラウド向けのアプリケーション
基盤
‒ AP サーバーやデータベースやメッセージキューなどのソフト
ウェアを1セットで提供する
‒ Springを使用することで仮想アプリケーションとクラウド アプ
リケーションの開発期間が最大 50 % 短縮
ERS(Enterprise Ready Server)
Webサーバ
Hyperic
(モニタリング tc Server
と監視)
RabbitMQ GemFire
(メッセージング) (データ管理)
16. ERS(Enterprise Ready Server)
• Web サーバーとロードバランスの機能を提供
‒ Apache HTTP Server を使用
‒ Apache と比較して最大 100% のパフォーマンスアップ
• 迅速にバグフィックスするサポートを提供
‒ Apache HTTP Server のサポートを提供していた Covalent 社
を SpringSource が買収
tc Server
ERS
tc Server
17. tc Server
• エンタープライズ向けにカスタマイズされた Tomcat
‒ 開発者に馴染みの Tomcat互換
‒ Springを利用して作成したアプリケーションを実行するのに適
した環境
• Tomcat + α
‒ Webベースの管理UI
‒ アプリケーションの設定とデプロイ管理
‒ パフォーマンスのモニターおよび状態の診断
tc
ERS
Server
Rabbit
tc MQ
ERS
Server
Rabbit
tc MQ
ERS
Server
18. RabbitMQ
• AMQP(Advanced Message Queuing Protocol) を実
装したメッセージキュー
• 多彩なメッセージングのパターンに対応
‒ point-to-point, publishsubscribe,routing...
• 多彩なプロトコル、言語(クライアント側)に対応
ERS
tc Server
RabbitMQ
Cloud時代のスケール
メッセージBus
20. Hyperic
• tc Serverの管理・監視・計測
‒ 他のミドルウェアや OS リソースも監視できる
• 管理
‒ ランタイムインスタンスの起動・停止
‒ JVM オプション・ JDBC データソースなどの設定
‒ Web アプリケーションのデプロイ
• 監視
‒ リソースの閲覧・監視・コントロール
‒ デッドロックの検知
‒ ガベージコレクションの時間
‒ JDBC データソースの監視
• 計測
‒ instrumented 版の Spring Framework を利用し、 bean のメ
ソッドの処理時間などを表示
24. Bean定義のプロファイル機能
• Bean定義をプロファイルという形でグループ化
‒ Bean定義の有効範囲を指定できる機能
<?xml version="1.0" encoding="UTF-8"?>
<beans ・・・>
<beans profile="test">
<bean id="dataSource"
class="org.springframework・・・DriverManagerDataSource">
</bean>
<bean ・・・
</beans>
<beans profile="production">
<jee:jndi-lookup id="dataSource"
jndi-name="・・・"/>
<bean ・・・
</beans>
</beans>
24
27. Webアプリで指定(1)
• Webアプリケーションでtestを指定
‒ ContextLoaderListenerの場合
web.xml
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</context-param>
27
28. Webアプリで指定(2)
• Webアプリケーションでtestを指定
‒ DispatcherServlet
web.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
・・・省略・・・
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>test</param-value>
</init-param>
・・・省略・・・
</servlet>
28
30. 組込みデータベースサポート
• 組込みデータベースをDIコンテナで管理
‒ DataSourceオブジェクトとしてアクセスすることが
できる機能
‒ データベースの起動/停止を意識することなくアプ
リケーションを実行可能
‒ メモリ上で起動、終了時には破棄
• テーブルやデータをSQLスクリプトファイルに記述
• Javaの軽量なデータベース
‒ HSQLDB(http://www.hsqldb.org/)
‒ H2(http://www.h2database.com/)
‒ Apache Derby(http://db.apache.org/derby/)
30
31. HSQLを利用
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
・・・
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:/META-INF/sql/ddl.sql"/>
<jdbc:script location="classpath:/META-INF/sql/data.sql"/>
</jdbc:embedded-database>
・・・
</beans>
<jdbc:embedded-database id="dataSource" type="H2">
※type ="DERBY"
31
33. Object/XMLマッピング連携
• OXM(Object/XMLマッピング )共通のイン
タフェースを定義
‒ Marshallerインタフェース
• Object→XML
‒ Unmarshallerインタフェース
• XML→Object
• 実装クラス
‒ JAXB(http://jcp.org/en/jsr/summary?id=222)
‒ Castor(http://castor.org/xml-framework.html)
‒ XMLBean(http://xmlbeans.apache.org/)
‒ JiBX(http://jibx.sourceforge.net/)
‒ XStream(http://xstream.codehaus.org/)
33
34. OXMサンプル
XML
:Customer
HTTPリクエスト
Http メソッドの引数
Spring
Message
Converter
MVC
HTTPレスポンス
メソッドの戻り値
XML
:Customer
34
35. Bean定義ファイル(JAXB利用)
<?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:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">
<oxm:jaxb2-marshaller id="marshaller">
<oxm:class-to-be-bound name="sample.customer.biz.domain.Customer" />
</oxm:jaxb2-marshaller>
<bean id="marshallingConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>
</bean>
</beans>
35
38. SimpleCacheManagerの利用
<bean id="cacheManager"
class="org.springframework.cache.support.
SimpleCacheManager">
<property name="caches">
<list>
<bean class="org.springframework.cache.
concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="users"/>
</bean>
<bean class="org.springframework.cache.
concurrent.ConcurrentMapCacheFactoryBean">
・・・省略・・・
</list>
</property>
</bean>
38
39. アノテーションの利用(1)
• cache:annotation-drivenタグを設定
<?xml version="1.0" encoding="UTF-8"?>
<beans
・・・省略・・・
xmlns:context="http://www.springframework.org
/schema/context"
xsi:schemaLocation="
・・・省略・・・
http://www.springframework.org/schema/cache
http://www.springframework.org
/schema/cache/spring-cache-3.1.xsd">
<cache:annotation-driven />
・・・省略・・・
39
40. アノテーションの利用(2)
@Cacheable(value = "users", key = "#userId")
public User findById(long userId){
・・・省略(データベースアクセスなど)・・・
:User
:User
userId="001"の場合
userId=003
userId=001
findById(..)
userId="009"の場合
:UserTBL
40
41. アノテーションの利用(3)
@CacheEvict(value = "users", key = "#user.id")
public void update(User user) {
・・・省略(データベースアクセスなど)・・・
:User
:User
userId="001"の場合 userId=003
delete
userId=001
update(..)
update
:UserTBL
41
42. Bean定義の利用
• キャッシュの設定をソースに書きたくな
い場合
<cache:advice id="cacheAdvice">
<cache:caching cache="users">
<cache:cacheable method="findById" key="#userId"/>
<cache:cache-evict method="update" key="#user.id"/>
・・・省略・・・
</cache:advice>
42
45. Bean定義ファイル
• task:annotation-drivenタグを設定
<?xml version="1.0" encoding="UTF-8"?>
<beans
・・・省略・・・
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
・・・省略・・・
http://www.springframework.org/schema/task
http://www.springframework.org/schema/
task/spring-task-3.1.xsd">
<task:annotation-driven/>
・・・省略・・・
45
48. スケジューリング
@Scheduled(fixedDelay = 5000)
public void bar() { ・・・
//属性は1つしか指定できない
属性 説明
cron形式で指定したスケジュールに従って実行する
cron
例)毎日12時に実行 cron = "0 0 12 * * ?"
指定されたミリ秒の間隔でメソッドを実行する
fixedDelay メソッドが実行されるまでの間隔は、前のメソッド
が終了した時点からカウントされる
指定されたミリ秒の間隔でメソッドを実行する
fixedRate メソッドが実行されるまでの間隔は、前のメソッド
が開始した時点からカウントされる
48
53. Spring利用時の問題点(昔)
• Webアプリで、様々なクラスが
ApplicationContextを生成してクラスを
getしていた
‒ ネット連載の初回しか見なかったのか?
• 何でもかんでもInterfaceを付加して
Injection(今でもちょっとある)
‒ 書籍や記事等で強調し過ぎた!?
• 「うちではそんな高度なフレームワーク
は使いこなせません」
‒ これはちょっと…
53
55. これからSpringを使うには
• ドメインモデリング重要
‒ 責務が明確で、分かり易く、変化に対応でき
るビジネスロジック
• トランザクションスクリプトからドメインモデル
へリファクタリングすることも、ひとつの手とし
はアリかも!?
• 例:トランザクション内に分散したValidationを
エンティティに移動する
‒ Springが得意とするレイヤ間の疎結合は、そ
の前提
‒ DDDは教材の1つとして有効
55
58. JavaEE
• JavaEE7
‒ 標準的なPaaSの提供を目指す
• クラウド環境に対応した自動プロビジョニングや、多岐にわ
たる参加者の管理モデルも提唱
‒ JDK(Project Lambda)
• JDK8 2013年
‒ 使い易くなるキャッシュ機能(JCache 1.0)
‒ 既存のAPIにはマルチテナント対応の拡張
(JPA2.1)
‒ コンポーネントの容易な組み合わせ(CDI 1.1)
PaaS
58
59. JDK8(Project Lambda)
• 関数型の採用
• マルチコア対応
(int x, int y) -> x + y
() -> 42
(String s) -> { System.out.println(s); }
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html
59
61. Contexts and Dependency Injection
• コンポーネントを簡単に組み合わせる仕
組み
‒ 仕様の異なるコンポーネントを、組み合わせ
るのが大変だった
• @Inject
‒ Web Beans
• JBoss Seam
JSF
EJB
JPA
CDI
61
64. Spring+αで十分
• Spring3.1
‒ Cache(JCache)
‒ DIxAOP(CDI)
• Scala
‒ 関数型(Lambda)
• Hibernate4
‒ マルチテナンシー(JPA)
新SSH
64
68. JSUG勉強会
• 是非ご参加下さい
‒ 2ヶ月に1回あれば良い程度に開催
‒ MLにてお知らせ
• http://groups.google.co.jp/group/jsug
• 発表者求む!
‒ ご連絡ください:staff@springframework.jp
68