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
80
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
300
決済サービスのSpring Bootのバージョンを2系に上げた話
b1a9id
0
150
Form認証で学ぶSpring Security入門
b1a9id
0
320
パラレルキャリアがもたらす相乗効果
b1a9id
1
1.3k
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
b1a9id
0
1.6k
ユニットテストのアサーション 流れるようなインターフェースのAssertJを添えて 入門者仕立て
b1a9id
1
130
Spring starterによるSpring Boot Starter
b1a9id
1
77
Other Decks in Programming
See All in Programming
MySQL9でベクトルカラム登場!PHP×AWSでのAI/類似検索はこう変わる
suguruooki
1
250
副作用と戦う PHP リファクタリング ─ ドメインイベントでビジネスロジックを解きほぐす
kajitack
3
480
構造化・自動化・ガードレール - Vibe Coding実践記 -
tonegawa07
0
150
DMMを支える決済基盤の技術的負債にどう立ち向かうか / Addressing Technical Debt in Payment Infrastructure
yoshiyoshifujii
4
640
「次に何を学べばいいか分からない」あなたへ──若手エンジニアのための学習地図
panda_program
3
660
iOS開発スターターキットの作り方
akidon0000
0
210
QA x AIエコシステム段階構築作戦
osu
0
210
Google I/O Extended Incheon 2025 ~ What's new in Android development tools
pluu
1
200
No Install CMS戦略 〜 5年先を見据えたフロントエンド開発を考える / no_install_cms
rdlabo
0
370
React 使いじゃなくても知っておきたい教養としての React
oukayuka
13
1.6k
Prompt Engineeringの再定義「Context Engineering」とは
htsuruo
0
110
The Modern View Layer Rails Deserves: A Vision For 2025 And Beyond @ RailsConf 2025, Philadelphia, PA
marcoroth
2
820
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Site-Speed That Sticks
csswizardry
10
720
The Invisible Side of Design
smashingmag
301
51k
It's Worth the Effort
3n
185
28k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
21k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
The Cost Of JavaScript in 2023
addyosmani
51
8.6k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
Documentation Writing (for coders)
carmenintech
72
4.9k
YesSQL, Process and Tooling at Scale
rocio
173
14k
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に思う事
神ってる
おわり