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
71
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
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
2
3.2k
Enterprise Web App. Development (1): Build Tool Training Ver. 5
knakagawa
1
110
エンジニア未経験が最短で戦力になるためのTips
gokana
0
260
Coding Experience Cpp vs Csharp - meetup app osaka@9
harukasao
0
720
アーキテクトと美学 / Architecture and Aesthetics
nrslib
12
3.3k
PsySHから紐解くREPLの仕組み
muno92
PRO
1
540
Going Structural with Named Tuples
bishabosha
0
200
AI Agents with JavaScript
slobodan
0
210
リアクティブシステムの変遷から理解するalien-signals / Learning alien-signals from the evolution of reactive systems
yamanoku
3
1.2k
データベースエンジニアの仕事を楽にする。PgAssistantの紹介
nnaka2992
9
4.5k
タイムゾーンの奥地は思ったよりも闇深いかもしれない
suguruooki
1
510
Go1.24 go vetとtestsアナライザ
kuro_kurorrr
2
820
Featured
See All Featured
Speed Design
sergeychernyshev
28
880
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
30k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Code Reviewing Like a Champion
maltzj
522
39k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Raft: Consensus for Rubyists
vanstee
137
6.9k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Facilitating Awesome Meetings
lara
54
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のマッピング情報
スピーカー交代