Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Spring starterによるSpring Boot Starter
Search
Ryosuke Uchitate
June 22, 2016
Programming
1
72
Spring starterによるSpring Boot Starter
JSUG勉強会 2016年その4 Spring I/O報告会(2016/6/22)
Ryosuke Uchitate
June 22, 2016
Tweet
Share
More Decks by Ryosuke Uchitate
See All by Ryosuke Uchitate
決済サービスのSpring Bootのバージョンを2系に上げた話
b1a9id
0
140
Form認証で学ぶSpring Security入門
b1a9id
0
290
パラレルキャリアがもたらす相乗効果
b1a9id
1
1.2k
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
b1a9id
0
1.5k
ユニットテストのアサーション 流れるようなインターフェースのAssertJを添えて 入門者仕立て
b1a9id
1
120
Spring超入門-Springと出会ってから1年半-
b1a9id
1
76
Other Decks in Programming
See All in Programming
サービスクラスのありがたみを発見したときの思い出 #phpcon_odawara
77web
4
630
AHC 044 混合整数計画ソルバー解法
kiri8128
0
330
Develop Faster With FrankenPHP
dunglas
2
3.2k
API for docs
soutaro
1
590
タイムゾーンの奥地は思ったよりも闇深いかもしれない
suguruooki
1
570
アプリを起動せずにアプリを開発して品質と生産性を上げる
ishkawa
0
2.6k
Building Scalable Mobile Projects: Fast Builds, High Reusability and Clear Ownership
cyrilmottier
2
260
小田原でみんなで一句詠みたいな #phpcon_odawara
stefafafan
0
320
いまさら聞けない生成AI入門: 「生成AIを高速キャッチアップ」
soh9834
15
4.5k
Defying Front-End Inertia: Inertia.js on Rails
skryukov
0
460
Code smarter, not harder - How AI Coding Tools Boost Your Productivity | Webinar 2025
danielsogl
0
120
AI Coding Agent Enablement - エージェントを自走させよう
yukukotani
13
5.8k
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
522
39k
We Have a Design System, Now What?
morganepeng
52
7.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Agile that works and the tools we love
rasmusluckow
328
21k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
How to Ace a Technical Interview
jacobian
276
23k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.1k
The Language of Interfaces
destraynor
157
24k
Being A Developer After 40
akosma
91
590k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
The Cult of Friendly URLs
andyhume
78
6.3k
Transcript
Boot starterが語るSpring bootの仕組み & Spring 5以降のReactiveな流れと フロントまわりの話 株式会社タグバンガーズ
横浜・設立6年目・Webアプリ開発・ほぼSpring案件
TAG BAR 月1ぐらい開催してる社外の方を招いて行う勉強会 connpass(http://tagbar.connpass.com/)にて募集
軽く自己紹介 佐々木 亜里沙(ささき ありさ) UIチーム・プロダクトチーム 北海道 内立 良介(うちたて りょうすけ) デベロップメントチーム
宮崎
Spring starterによる Spring Boot Starter 株式会社タグバンガーズ 内立良介
AGENDA Spring I/O 2016の様子 セッション概要 セッションの解釈(@AutoConfiguration, Starter) 開発時の便利機能の紹介
Spring I/O 2016 開催地:スペイン・バルセロナ セッション数:2日間で48セッション 参加者:26カ国、475人(うち日本からの参加:8人)
Session
AFTER EVENT PARTY
セッションの概要
CUSTOMIZE YOUR SPRING BOOT EXPERIENCE BY WRITING YOUR OWN SPRING
BOOT STARTER https://github.com/michael-simons/springio2016 ベースとなるセッション
Custom Spring Boot Starter –part1- Spring Boot Starterには様々な種類がある Integration、Template Engines、Validationなど
プロジェクトを作るときは以下のURLから作ると簡単に作れる http://start.spring.io
None
Custom Spring Boot Starter –part2- 標準的なAutoConfigurationに使用 @ConditionalOnClass @AutoConfigureAfter、@AutoConfigureBefore @Bean
Custom Spring Boot Starter –part3- その他の条件指定・順序決め @ConditionalOnProperty @ConditionalOnMissingBean @Order
セッションの解釈
@SpringBootApplicationって何者?
@SpringBootApplication 3つのアノテーションを1つにまとめたもの @EnableAutoConfiguration 自動設定を有効にする @ComponentScan このクラスのパッケージ配下で@Component, @Service, @Controller,@RestController, @NamedつきのクラスをDIコンテナに登録する @Configuration
このクラス自体をBean定義可能にする
@SpringBootApplicationの流れ (@EnableAutoConfigurationのみ)
1.DIコンテナ(ApplicationContext)作成
2.BeanFactory作成
3.spring.factries読込、 BeanDefinition取捨選択、並び替え
4.Bean登録
さあ、starterを作りましょう!!!
starterがやってくれること アプリケーションの開発に必要な依存関係を追加 ライブラリを使うために必要な設定を追加
ネーミングルール ”spring-boot”ではじまる名前をつけてはいけない!
社内で作ったライブラリなどを starterにすれば。。。
面倒な設定なしに使える!!
突然ですが、 spring.factriesって知ってますか?
spring.factories org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.mycorp.libx.autoconfigure.LibXAutoConfiguration, com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration このように書けば、Spring Bootが@EnableAutoConfigurationの対象と認識してくれ る。 デフォルトのパスは、META-INF/spring.factoriesとなっている。
作ったstarterを読み込ませたいなら
作ったstarterのAutoConfigurationのパスを META-INF/spring.factoriesに書けばいい。
というのはわかったけど。。。
依存関係があるから適用する 順序を指定したい。
安心してください! 順序指定できますよ!
インポート順序を指定するための アノテーション @Order 引数に指定した順序で優先順位が決まる。最も小さい値が優先度が最も高 い。 @AutoConfigureBefore 指定したauto-configurationクラスより前に適用する。 @AutoConfigureAfter 指定したauto-configurationクラスより後に適用する。
依存関係にあるクラスの有無で インポートするかどうか指定したい。
安心してください! 指定できますよ!
Conditionアノテーションを使おう!
Conditonアノテーション @ConditionalOnClass(@ConditionalOnMissingClass) 指定したクラスがクラスパス上にあったら(なかったら)有効 @ConditionalOnBean(@ConditionalOnMissingBean) 指定したBeanがあったら(なかったら)有効 @ConditionalOnProperty 指定したプロパティがあったら有効
@ConditionalOnResource 指定したリソースがあったら有効 @ConditionalOnWebApplication(@ConditionalOnNotWebApplication) Webアプリケーションであったら(なかったら)有効
デモアプリケーションをみてみよう!!
テンプレートで <banner:show />を指定すれば、 viewでbannerイメージを表示して くれるstarter セッションで行われたデモ
プロジェクトの構成
demoの pom.xml
AutoConfigurationクラス
spring.factories
開発時の便利機能の紹介
spring-boot-starter-actuator システムの状態やメトリクスを確認できる 主なエンドポイント autoconfig auto-configurationの候補と自動設定された理由・されなかった理由 beans DIコンテナに登録されているBeanの一覧 metrics
メトリクス情報が見れる mappings @RequestMappingのマッピング情報
スピーカー交代