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
73
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
決済サービスのSpring Bootのバージョンを2系に上げた話
b1a9id
0
140
Form認証で学ぶSpring Security入門
b1a9id
0
280
パラレルキャリアがもたらす相乗効果
b1a9id
1
1.2k
Amazon Cognito使って認証したい?それならSpring Security使いましょう!
b1a9id
0
1.5k
ユニットテストのアサーション 流れるようなインターフェースのAssertJを添えて 入門者仕立て
b1a9id
1
120
Spring starterによるSpring Boot Starter
b1a9id
1
71
Other Decks in Programming
See All in Programming
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
520
責務と認知負荷を整える! 抽象レベルを意識した関心の分離
yahiru
4
620
AIの力でお手軽Chrome拡張機能作り
taiseiue
0
170
さいきょうのレイヤードアーキテクチャについて考えてみた
yahiru
3
750
Introduction to kotlinx.rpc
arawn
0
700
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2.2k
Lottieアニメーションをカスタマイズしてみた
tahia910
0
130
Open source software: how to live long and go far
gaelvaroquaux
0
640
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
560
メンテが命: PHPフレームワークのコンテナ化とアップグレード戦略
shunta27
0
120
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
150
JavaScriptツール群「UnJS」を5分で一気に駆け巡る!
k1tikurisu
9
1.8k
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
RailsConf 2023
tenderlove
29
1k
Building Your Own Lightsaber
phodgson
104
6.2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
330
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Writing Fast Ruby
sferik
628
61k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
9
450
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に思う事
神ってる
おわり