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
tparseでgo testの出力を見やすくする
utgwkk
2
330
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.4k
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
160
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
470
Go コードベースの構成と AI コンテキスト定義
andpad
0
150
Canon EOS R50 V と R5 Mark II 購入でみえてきた最近のデジイチ VR180 事情、そして VR180 静止画に活路を見出すまで
karad
0
140
[AtCoder Conference 2025] LLMを使った業務AHCの上⼿な解き⽅
terryu16
6
990
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
270
AtCoder Conference 2025「LLM時代のAHC」
imjk
2
620
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
510
Deno Tunnel を使ってみた話
kamekyame
0
290
AI Agent Dojo #4: watsonx Orchestrate ADK体験
oniak3ibm
PRO
0
120
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Darren the Foodie - Storyboard
khoart
PRO
0
2k
The Curse of the Amulet
leimatthew05
0
6.6k
RailsConf 2023
tenderlove
30
1.3k
How to Think Like a Performance Engineer
csswizardry
28
2.4k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
30 Presentation Tips
portentint
PRO
1
180
Navigating Weather and Climate Data
rabernat
0
60
HDC tutorial
michielstock
1
290
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
KATA
mclloyd
PRO
33
15k
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に思う事
神ってる
おわり