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
ginza-ruby-kaigi-01
Search
onigra
August 05, 2017
Technology
4
970
ginza-ruby-kaigi-01
https://ginzarb.github.io/kaigi01/
onigra
August 05, 2017
Tweet
Share
More Decks by onigra
See All by onigra
THE GOAL
onigra
3
60
devsumi-2024-summer
onigra
4
1.7k
第一種低層住居専用地域
onigra
0
230
jaws-ug-ecspresso-meetup-20230808
onigra
0
1.7k
PHP-CS-FixerとかAtomとか
onigra
1
1.3k
プログラミング初心者でも始められるコミュニティへの参加と貢献
onigra
4
730
Techblog Deep Dive Meetup #1
onigra
0
1.8k
とある業務オペレーション自動化の話
onigra
0
950
about tsudura
onigra
0
250
Other Decks in Technology
See All in Technology
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
300
Terraform Stacks入門 #HashiTalks
msato
0
360
Why App Signing Matters for Your Android Apps - Android Bangkok Conference 2024
akexorcist
0
130
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
3
620
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.8k
組織成長を加速させるオンボーディングの取り組み
sudoakiy
2
200
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
AGIについてChatGPTに聞いてみた
blueb
0
130
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
4
230
SREが投資するAIOps ~ペアーズにおけるLLM for Developerへの取り組み~
takumiogawa
1
420
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Adopting Sorbet at Scale
ufuk
73
9.1k
Building Adaptive Systems
keathley
38
2.3k
Site-Speed That Sticks
csswizardry
0
28
Visualization
eitanlees
145
15k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Optimizing for Happiness
mojombo
376
70k
Documentation Writing (for coders)
carmenintech
65
4.4k
Teambox: Starting and Learning
jrom
133
8.8k
Gamification - CAS2011
davidbonilla
80
5k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
For a Future-Friendly Web
brad_frost
175
9.4k
Transcript
Spring Frameworkͱൺֱֶͯ͠Ϳɺ WebΞϓϦέʔγϣϯϑϨʔϜϫʔΫͷ ׂ ͗Μ͟Rubyձٞ 01 2017-08-05 @onigra_
Yudai Suzuki / ླ༤େ @onigra_ onigra
͜ͷΛ͢Δʹ͋ͨͬͯ • ϓϩάϥϚʔͱͯ͠ࣄ͢Δ্Ͱɺ ॳΊͯͪΌΜͱֶΜͩͷ͕Ruby • ࣄͰ͏Web Application Frameworkɺ ΄ͱΜͲRailsͩͬͨ
Railsʹֶ͍ͭͯΜͰߦ͘͏ͪʹ • ใΛूΊͯΈΔͱɺRailsʹׂͱ ൷͕ଟ͍͜ͱʹؾͮ͘
ྫ • MVCͷఆٛʹ͍ͭͯ • ActiveRecord
ͳΔ΄Ͳ
ࠓͷ4݄ʹస৬
http://rubykaigi.org/2017/sponsors • http://rubykaigi.org/2017/sponsors
స৬ͯ͠ Java + Spring Frameworkʹ৮ΕΔ • ਖ਼֬ʹSpring Boot • Spring
BootɺSpring Frameworkͷ͍ͭ͘ ͔ͷϞδϡʔϧΛ౷߹ͯ͠ɺWebΞϓϦ έʔγϣϯΛߏஙͨ͘͢͠͠ͷ
Spring FrameworkΛ௨͠ ൷ͷ༰Λ վΊͯཧղ͢Δ
͜ͷൃදͷதͰ͍͑ͨ͜ͱ • ࢲͷΑ͏ʹɺϓϩάϥϛϯάWeb։ൃΛ RubyɺRails͔Β͡Ίͨํʑʹɺ ҟͳΔݴޠͷϑϨʔϜϫʔΫͷ ઃܭࢥύϥμΠϜʹ৮ΕΔ͜ͱʹΑΓɺ ৽͍͠ݟΛಘΔػձʹͳ͍͚ͬͯͨͩΕ ͍Ͱ͢
ҙ • ຊൃදͰপʹೖΓࠐΉͷΛආ͚ΔͨΊɺҎԼʹ͍ͭͯ ਂ͘౿ΈࠐΜͩ͠·ͤΜ • υϝΠϯۦಈઃܭ(DDD) • ΤϯλʔϓϥΠζΞϓϦέʔγϣϯΞʔΩςΫνϟύλʔ ϯ(PofEAA) •
͋͘·ͰRuby on RailsͱSpring Frameworkͷػೳͷൺֱͱ ͍͏؍Ͱ͓͠·͢
ࠂ
גࣜձࣾΤεΤϜΤεͰɺ ษڧձͷձఏڙΛ͡Ί·͢ • ࠷େͰ136໊·Ͱऩ༰ՄೳͳձΛఏڙͰ͖· ͢ • ৄࡉ@onigra_·Ͱ͓͍߹Θ͍ͤͩ͘͞ • ॴେ(ࣳެԂ) •
https://twitter.com/onigra_
Elastic Leadershipͷ ΠϕϯτΓ·͢ • ฐࣾͨͳ͢ͳ͓(@sunaot)͕ΤοηΠΛدߘ͠ ͨɺElastic LeadershipͷΠϕϯτΛߦ͍·͢ • ήετʹϨδϟϦʔϫʔΫε߹ಉձࣾͷ Shinichiro
OGAWA(@conceal_rs)͞ΜΛ͓ট͖͠ ·͢ • ͪ͜Βৄࡉ͕ܾ·Γ࣍ୈɺSNSʹͯࠂ͠·͢
σʔλΞΫηε
σʔλΞΫηε • Spring FrameworkʹɺRDBMSͷσʔλ ӬଓԽʹΞΫηε͢ΔͨΊͷϥΠϒϥϦͱ ͯ͠ɺSpring Data͕ଘࡏ͢Δ • Spring DataɺRDBMSҎ֎ͷσʔλιʔε
ʹରԠ࣮͕ͨ͠ଘࡏ͢Δ
Spring Data • https://projects.spring.io/spring-data/ • MongoDB • LDAP • ֎෦Web
API • Redis • Elasticsearch • etc…
Repository • Spring Core͕ఏڙ͍ͯ͠Δ • υϝΠϯΦϒδΣΫτͱσʔλΞΫηεͷ࣮Λ ্ͨ͠ͰɺRepositoryͦΕΒͷհΛߦ͏ • Spring Core͕ఏڙ͢ΔRepositoryʹର͠ɺ
Spring DataͷΛTemplateͱݺͿ
ϑϨʔϜϫʔΫͱͯ͠ͷ ActiveRecordͷσϝϦοτ • ϞσϧͱDB͕ີ݁߹ • Modelͱσʔλϕʔεઃܭ͕݁߹ͯ͠͠·͏ • ΦϒδΣΫτ͕σʔλϕʔεςʔϒϧͱରԠ͠ ͍ͯΔͱActiveRecordඇৗʹ༗ޮ •
σʔλϕʔεͷઃܭ͕ɺ͍͔ʹActiveRecordͰѻ͍ ͘͢͢ΔʹͲ͏͢Δ͔ɺͱ͍͏ํʹͳΓ͕ͪ
RepositoryͱTemplateΛ ༻͍࣮ͨ • Interfaceͷఆٛ • Interfaceʹର͠ɺσʔλιʔε͝ͱʹɺσʔ λΞΫηεͷ۩ମతͳ࣮Λߦ͏
RepositoryͱTemplateΛ ༻͍࣮ͨ
Կ͕خ͍͔͠ʁ • RepositoryͰఆٛͨ͠ΠϯλʔϑΣʔεͱɺ Templateͷ࣮Λ͚Δ͜ͱʹΑͬͯɺ ϏδωεϩδοΫͱ࣮ͷ͕Ͱ͖Δ • σʔλΞΫηεͷ࣮ΛΓସ͑Δ͚ͩͰɺΞϓ Ϧέʔγϣϯͷมߋ͕ྃ͢Δ • ϏδωεϩδοΫʹର࣮ͯ͠ͷมߋ͕༰қʹͳΔ
ActiveResource • ֎෦ͷWeb APIΛɺAcriveRecordΦϒδΣΫ τͷΑ͏ʹѻ͏͜ͱ͕Ͱ͖ΔΑ͏ʹͳΔgem • Rails2, 3ʹCoreʹ͕͋ͬͨɺ4Ͱ֎Εͨ
RubyͰRepository • rom-rb • http://rom-rb.org/ • Ruby Object Mapper •
HANAMIͷ෦Ͱ༻͞Ε͍ͯΔ • http://hanamirb.org/
Callbackͱ ΞεϖΫτࢦϓϩάϥϛϯά
ΞεϖΫτࢦϓϩάϥϛϯά ͱʁ • Aspect Oriented Programming • ΦϒδΣΫτͷ୯ҐͰ͏·͘Ͱ͖ͳ͍ɺ ΦϒδΣΫτؒΛ·͙ͨػೳΛʮہ໘ʯͱ͍͏ ୯Ґ͝ͱʹϞδϡʔϧԽ͢ΔϓϩάϥϛϯάύϥμΠ
Ϝ • ΦϒδΣΫτؒΛ·͙ͨػೳͷ͜ͱΛ ʮCrosscutting Concern(ԣஅతؔ৺ࣄ)ʯͱݺͿ
Crosscutting Concernͷྫ • Logging • Caching • Error Handling •
Transaction
ΞεϖΫτࢦϓϩάϥϛϯά Ͱ༻͍ΒΕΔ༻ޠ • Join Point • ΞϓϦέʔγϣϯ࣮ߦதͷɺ͋Δಛఆͷʮ࣌ʯΛࢦ͢ • Advice •
Join PointͰ࣮ߦ͞ΕΔॲཧ • Pointcut • Join Pointͷఆٛ
Spring AOP
Spring AOPͷ͍ํ • Join PointΛܾΊͯPointcutͱͯ͠ఆٛ͠ɺ PointcutͰߦ͏࣮ॲཧͰ͋ΔAdviceΛఆٛ͢ Δ • ಛఆͷΫϥεͷಛఆͷϝιου͕࣮ߦ͞ΕΔ લޙʹɺ࣮ߦ͢ΔॲཧΛఆٛ͢Δ
Pointcutͷఆٛ @Aspect public class RepositoryPointcut { // UserRepositoryͷsaveϝιου͕࣮ߦ͞ΕΔ࣌(Join Point)ΛPointcutͱͯ͠ఆٛ @Pointcut("execution(*
com.onigra.springaopsample.repository..UserRepository+.save(..))") public void save() {} }
Adviceͷఆٛ @Aspect @Component public class LoggingAdvice { private static final
Logger LOGGER = LoggerFactory.getLogger(LoggingAdvice.class); @Before(“com.onigra.springaopsample.aspect.UserPointcut.save()") public void beforeSave(JoinPoint joinPoint) { LOGGER.info("UserRepositoryͷsaveϝιου࣮ߦલʹϩάΛग़ྗ͢Δ"); } }
RailsͰݴ͏ͱCallback͔ͳ
AOPͱCallback ࣅͯඇͳΔͷ
RailsͷCallback • ActiveSupport::Callbacks#run_callbacksʹ ϒϩοΫΛ͢͜ͱͰ࣮ݱ • Rubyͷݴޠͱͯ͠ͷػೳΛར༻͍ͯ͠Δ
Spring AOP • όΠτίʔυม • ίϯύΠϧޙͷόΠφϦΛվม͠ɺॲཧΛࠩ ͠ࠐΉ
TransactionͰͷAOP • RailsɺActiveRecord::Transactions.transactionʹϒϩοΫΛ͢ • Spring FrameworkɺTransactionΛ͔͚͍ͨ ϝιουʹର͠ɺTransactionalΞϊςʔγϣϯ(Ճใ)Λ༩ • RailsͷΑ͏ʹɺϓϩάϥϜͷதͰ੍ޚΛߦ͏ํ๏ʹର͠ɺʮಛఆ ͷϝιουͷ࣮ߦ࣌ʹτϥϯβΫγϣϯΛ͔͚Δʯͱ͍͏
એݴΛߦ͏͜ͱΛɺʮએݴతτϥϯβΫγϣϯʯͱݺͿ • ※ϓϩάϥϛϯάʹΑΔτϥϯβΫγϣϯཧͰ͖Δ
MVCͱ ϨΠϠʔυΞʔΩςΫνϟ
ΞϓϦέʔγϣϯͷ ϨΠϠʔԽͷͱ MVC(MVC2, MVC Model2)ͷ શ͘ผͷʂʂʂ
Railsͷׂͷɺ ΞϓϦέʔγϣϯͷϨΠϠʔԽͷͱɺ MVCͷ͕ࠞͬͯ͟͠·͍ͬͯΔ ʢ͜ͱ͕ଟ͍ؾ͕͢Δʣ
͞Βʹͦ͜ʹDDDʹ͓͚ΔαʔϏεͱ PofEAAͷαʔϏεͷ͕ࠞͬͯ͟ େมͳ͜ͱʹ…
ΞϓϦέʔγϣϯઃܭʹ͓͚Δ ϨΠϠͷ֓೦ͱ • ݩClient-Server Modelͷߟ͑ํ • Webʹߟ͑ํ͕Ԡ༻͞Εɺൃల͖ͯͨ͠ • ͔͠͠ɺΞʔΩςΫνϟจݙʹΑ༷ͬͯʑ ͳϨΠϠʔͷ໊শ͕ଘࡏ͢ΔͷͰҙ͕ඞཁ
ΫϥΠΞϯταʔόϞσϧʹ͓ ͚Δ3ΞʔΩςΫνϟ • ϓϨθϯςʔγϣϯ: UI • ΞϓϦέʔγϣϯ: ϏδωεϩδοΫ • σʔλ:
σʔλΞΫηε
PofEAAʹ͓͚Δ3ΞʔΩςΫ νϟ • ϓϨθϯςʔγϣϯ: UI • υϝΠϯ: ϏδωεϩδοΫ • σʔλιʔε:
σʔλΞΫηε
͜ͷجຊͱͳΔ3Ͱ ߟ͑ͯݟΔ
Spring Framework • ϓϨθϯςʔγϣϯ • View(Template Engine(ThymeLeaf)) • Controller(Spring MVC)
• υϝΠϯ • Service(Spring Core) • Repository(Spring Core) • σʔλιʔε • Template(Spring Data) • ͦΕͧΕͷ͔ΒModelΛѻ͏
Spring Framework https://yo1000.gitbooks.io/self-study-spring/content/chapter1.html
Ruby on Rails • ϓϨθϯςʔγϣϯ • View(ActionView, View Template Engine(ERB))
• Controller(ActionController) • υϝΠϯ • Model(ActiveRecord, ActiveModel) • σʔλιʔε • Model(ActiveRecord)
༷ʑͳϨΠϠʔԽͷύλʔϯ͕ ଘࡏ͢Δ • ΞϓϦέʔγϣϯʹΑͬͯɺదͳϨΠϠԽ ҟͳΔ • จ຺ΞʔΩςΫνϟʹΑͬͯɺ໊শ͕ඃͬ ͨΓҟͳͬͨΓ૿͑ͨΓ͢Δͷʹҙ • ϨΠϠͷҙຯΛཧղ͠ɺదͳϨΠϠΛબ
͠Α͏
·ͱΊ
2ͭͷϑϨʔϜϫʔΫΛ ൺֱͯ͠ • RailsϢʔεέʔεΛߜͬͯɺׂΓͬͨઃܭʹ͍ͯ͠Δ • ͦͷɺϚον͠ͳ͍Ϗδωεཁ݅ʹΑͬͯɺਏΈવग़Δ • Spring Frameworkɺ͡Ί͔Β͍ϢʔεέʔεʹରԠ͢Δ ͜ͱΛࢦ͍ͯ͠Δ
• ෳࡶͳϏδωεཁ݅ɺظతͳ֦ுͷՄೳੑΛߟ͑Δͱɺ Spring Framework༏Εͨબࢶ • ҰํͰɺ͋Β͔͡Ίཧղ͓͖ͯ͘͠จ຺ɺ͕ࣝଟ͍
2ͭͷϑϨʔϜϫʔΫΛ ൺֱͯ͠ • ઃܭͷ෯Λ٘ਜ਼ʹͭͭ͠ɺ֮͑Δ͜ͱΛݮΒͯ͠։ ൃΛ্͛Δ͜ͱ͕Ͱ͖ΔActiveRecordΓศ ར • ໊শɺจ຺Λཧղ্ͨ͠ͰΞʔΩςΫνϟύλʔ ϯΛ͍͜ͳ͢ͷେม͔ͩΒɺେʹMVCͱ͍͏ ྨΛͨ͠ͷͰʁ
• ͦ͏ߟ͑ΔͱDHHϚδͰ͍͢͝…
ฏຌͳ݁Ͱ͕͢ • ͲͪΒͷํ͕༏Ε͍ͯΔͱ͍͏Ͱͳ͘ɺ ϑϨʔϜϫʔΫΞʔΩςΫνϟͷࢥͱ ϢʔεέʔεΛཧղ͠ɺదͳٕज़બఆΛͯ͠ ͍͖·͠ΐ͏
ҟͳΔݴޠͷϑϨʔϜϫʔΫ ઃܭࢥʹ৮ΕΔ͜ͱɺ ઃܭͷ෯Λ͛Δ͜ͱʹͭͳ͕Δ