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超入門-Springと出会ってから1年半-
Search
Ryosuke Uchitate
December 03, 2016
Programming
1
82
Spring超入門-Springと出会ってから1年半-
JJUG CCC 2016 Fall(2016/12/3)
Ryosuke Uchitate
December 03, 2016
Tweet
Share
More Decks by Ryosuke Uchitate
See All by Ryosuke Uchitate
キャッシュレス決済のプロダクトから決済基盤への進化
b1a9id
0
390
決済サービスのSpring Bootのバージョンを2系に上げた話
b1a9id
0
150
Form認証で学ぶSpring Security入門
b1a9id
0
340
パラレルキャリアがもたらす相乗効果
b1a9id
1
1.3k
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
b1a9id
0
1.7k
ユニットテストのアサーション 流れるようなインターフェースのAssertJを添えて 入門者仕立て
b1a9id
1
140
Spring starterによるSpring Boot Starter
b1a9id
1
82
Other Decks in Programming
See All in Programming
生成AI時代を勝ち抜くエンジニア組織マネジメント
coconala_engineer
0
37k
開発に寄りそう自動テストの実現
goyoki
2
1.7k
PostgreSQLで手軽にDuckDBを使う!DuckDB&pg_duckdb入門/osc25hi-duckdb
takahashiikki
0
230
CSC307 Lecture 02
javiergs
PRO
1
740
Denoのセキュリティに関する仕組みの紹介 (toranoana.deno #23)
uki00a
0
210
tparseでgo testの出力を見やすくする
utgwkk
2
340
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
580
Go コードベースの構成と AI コンテキスト定義
andpad
0
150
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
300
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
8
3.5k
Basic Architectures
denyspoltorak
0
150
公共交通オープンデータ × モバイルUX 複雑な運行情報を 『直感』に変換する技術
tinykitten
PRO
0
180
Featured
See All Featured
Docker and Python
trallard
47
3.7k
The Curse of the Amulet
leimatthew05
0
6.6k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
350
エンジニアに許された特別な時間の終わり
watany
106
220k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
80
Documentation Writing (for coders)
carmenintech
77
5.2k
SEO for Brand Visibility & Recognition
aleyda
0
4.1k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
72
The SEO identity crisis: Don't let AI make you average
varn
0
43
Optimizing for Happiness
mojombo
379
70k
Google's AI Overviews - The New Search
badams
0
880
The Cult of Friendly URLs
andyhume
79
6.7k
Transcript
Spring超入門 -Springと出会ってから1年半-
mi presento ▪名前:内立良介(うちたてりょうすけ) ▪あだ名:ジェンダ・フリ男 男性と女性の狭間(性格的な意味で) ▪趣味:ファッション・パン屋巡り 靴に夢中・クロワッサン ▪特技:バスケ 初めてから15年くらい? ▪所属:Tagbangers
通称タグバン
Tagbangers ▪場所:横浜市神奈川区 最寄りは横浜駅(徒歩15分) ▪社則 業務時間・交通費・通勤費・給与の4つのみ ▪飲み会 社長いじり ▪勉強会:TAG BAR 初回からちょうど1年
AGENDA 1. 今回作ったWebアプリケーションの紹介 2. Springについて 3. ソースコードの解説 4. おまけ
1.今回作ったWebアプ リケーションの紹介
利用者管理アプリ • 新規登録機能 • 編集機能 • 一覧表示機能 • 削除機能
新規登録機能の 構成図
None
え?
ビジネスロジックって よく聞くけど何だ?
訳すと、業務の論理?
ビジネスロジックは、データベースとユーザ ーインタフェース間の情報のやりとりを制御 する手順といったようなものを指す(技術的 でない)用語である。(Wikipediaより)
一言で言うと
システム固有の お仕事部分
具体例 • 画面から商品単価入力すると、税込でDBに登録す る処理 • 電話番号が「1234567」ではなく「123-4567」の フォーマットで入力されているかのチェックする処 理
よし、スッキリ!次
2.Springについて
Springって何? • Javaのフレームワーク • DI(依存性の注入)はとても重要 • さまざまなプロジェクト
え?(本日2回目)
DI(Dependency Injection):依存性の注入
意味わからん
DI • DIとは、Springが管理しているインスタンスの Beanをちょうだいとお願いしてDIコンテナから該 当箇所に注入してもらうこと DIコンテナ (Beanを管理している箱) Bean Bean Bean
Bean UserCreateController.java Beanちょうだい Bean
Bean:豆
え?
豆ってどういうこと?
調べて見ると、 コーヒー豆からきてるっぽ い
Java(ジャワ) • ジャワコーヒーはインドネシアのジャ ワ島を産地とするコーヒー豆。( WikiPediaより)
Beanも日本語訳は無 視
アドバイス
日本語訳に とらわれ過ぎない
さらにもう1つ
アノテーション=注釈 って訳さない方がよい
コメントとの違い何? ってなりました
経験談はこれくらいで 次
Spring Bootとは?
Spring Boot-1- • Spring Frameworkでアプリケーションを簡単に作 るための仕組み • 2014年4月にver1.0がリリースで1.4.2が最新 • XMLやJavaConfigでのBean定義、ログの設定など
が不要
Spring Boot-2- • mavenに限って言えば、pom.xmlに書くだけで依存 関係を解決してくれる • 良くも悪くもどういう動きをしているかよく知らな くてもそれなりに堅牢なアプリは作れる
どれくらい簡単かと 言うと
Spring Initializrでぽちぽちすれば、 基本設定で構築されたプロジェク トを作ることができる
Spring Initialize?
Spring Initializr https://start.spring.io/
Spring Bootの 重要な仕組み
AutoConfigure • Springが提供しているプロジェクトやOSSを利用す る時に必要なBean定義をSpring Bootが自動で行っ てくれる仕組みのこと 面倒なセットアップや設定がほとんど不要になる pom.xmlに書いたものを Bean登録する処理など
自動設定を有効にするには? @SpringBootApplication public class Application { public static void main(String[]
args) { SpringApplication.run(Application.class, args); } } このクラスに@Configurationを付与する
でも、、、
@Configurationないじゃん! @SpringBootApplication public class Application { public static void main(String[]
args) { SpringApplication.run(Application.class, args); } }
@SpringBootApplication に含まれている
はず。でも、、、
@Configurationないじゃん! @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = @Filter(type
= FilterType.CUSTOM, classes = TypeExcludeFilter.class)) public @interface SpringBootApplication { あやしい
見つけた! @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration {
@SpringBootApplicationは3 つのアノテーションを 1つにまとめたもの
@SpringBootApplication • @EnableAutoConfiguration • 自動設定を有効にする • @ComponentScan • このパッケージ配下で@Component、 @Service、@Controller、@RestController、
@Namedが付与されているクラスをDIコンテ ナに登録する • @SpringBootConfiguration • このクラス自体をBean定義可能にする
自動設定が有効に なるまでの流れ
1.DIコンテナ(ApplicationContext)作成
2.BeanFactory作成
3.spring.factories読込、 BeanDefinition取捨選択、並び替え
4.Bean登録
3.ソースコードの解説
pom.xml •プロジェクトに関する情報が書かれている •使いたいstarterやOSSの情報をかくとをレポジトリ から取ってくる <dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
... </dependencies>
@Controller •Controllerだよって教える •これを書くとSpring Bootで自動設定の対象となって Bean登録される
@RequestMapping •create.htmlのactionで指定したパスに一致している パスにマッピングされる •Spring Boot 1.4からは@GetMappingや @PostMapping、@DeleteMappingなども登場して いる @GetMapping =
@RequestMapping(method = RequestMethod.GET)
@Validated •formの入力値チェックをしてもらいたいときにつけ る @PostMapping public String save( @Validated @ModelAttribute(name =
FORM_MODEL_KEY) UserCreateForm form, BindingResult errors, RedirectAttributes redirectAttributes) {
formの中にformがある ときのエラーチェック
UserCreateForm.java @Getter @Setter public class UserCreateForm implements Serializable { @NotNull
private String name; private Integer age; @NotNull private Gender gender; @Valid private AddressForm address; public UserCreateRequest toUserCreateRequest() { return new UserCreateRequest(getName(), getAge(), getGender(), Address.generateAddress(getAddress())); } } @ValidをつけるとFormのなかの Formの入力値チェックをする
Springに思う事
神ってる
おわり