Save 37% off PRO during our Black Friday Sale! »

今こそ知りたいSpring DI×AOP / spring-di-aop-for-every-developers

5dbaf4015e7f249ab21b195ced8e9e46?s=47 Masatoshi Tada
December 16, 2020

今こそ知りたいSpring DI×AOP / spring-di-aop-for-every-developers

#jsug Spring Fest 2020での発表資料です。
Springの根幹機能はDIとAOPです。しかし、普通に使っているだけだと「@Autowiredを付ければ代入されるんでしょ?」「@Transactionalを付ければ割り込みされるんでしょ?」くらいの曖昧な理解に陥りがちです。
このセッションでは、DIやAOPの重要機能や仕組みを丁寧に解説します。Springの根幹を理解して、初級者から中級者へ、中級者から上級者へステップアップしましょう!

※発表では無かった内容を[おまけ]として加えました

5dbaf4015e7f249ab21b195ced8e9e46?s=128

Masatoshi Tada

December 16, 2020
Tweet

Transcript

  1. (C) CASAREAL, Inc. All rights reserved. KTVH ࠓͦ͜஌Γ͍ͨ 4QSJOH%*º"01 ג

    ΧαϨΞϧଟాਅහ ೥݄೔ +46(4QSJOH'FTU 1
  2. (C) CASAREAL, Inc. All rights reserved. KTVH ͜ͷηογϣϯʹ͍ͭͯ ▸ 4QSJOHͷجૅதͷجૅͰ͋Δʮίϯςφʯʮ%*ʯ

    
 ʮ"01ʯʹ͍ͭͯɺ෼͔Γ΍͘͢ղઆ͠·͢ ▸ جຊతʹॳ৺ऀ޲͚Ͱ͕͢ɺ4QSJOHΛར༻͍ͯ͠Δ 
 શͯͷํ͕ର৅Ͱ͢ ▸ 4QSJOH'SBNFXPSL4QSJOH#PPU 
 Ͱݕূ͍ͯ͠·͢ ▸ όʔδϣϯ͕ҟͳΔ৔߹ɺ಺༰΋ҟͳΔՄೳੑ͕͋Γ·͢ 2
  3. (C) CASAREAL, Inc. All rights reserved. KTVH ࣗݾ঺հ ▸ ଟాਅහʢ!TVLF@NBTBʣ

    ▸ ݚमτϨʔφʔ!ΧαϨΞϧ ▸ +BWB4QSJOH(PMBOH 
 1ZUIPO,VCFSOFUFT ▸ 7.XBSFೝఆߨࢣ ▸ ೔ຊ4QSJOHϢʔβձελοϑ 3
  4. (C) CASAREAL, Inc. All rights reserved. KTVH ໨࣍ ᶃ 4QSJOHͷίϯςφͱ͸

    ᶄ %*ͱ͸ ᶅ είʔϓͱ͸ ᶆ ϓϩΩγͱ͸ ᶇ "01ͱ͸ 4
  5. (C) CASAREAL, Inc. All rights reserved. KTVH ໨࣍ ᶃ 4QSJOHͷίϯςφͱ͸

    ᶄ %*ͱ͸ ᶅ είʔϓͱ͸ ᶆ ϓϩΩγͱ͸ ᶇ "01ͱ͸ 5
  6. (C) CASAREAL, Inc. All rights reserved. KTVH 4QSJOH͸ίϯςφΛ͍࣋ͬͯΔ ▸ ίϯςφʹΠϯελϯεͷೖΕ෺

    ▸ #FBOʹίϯςφͰ؅ཧ͞ΕͨΠϯελϯε 6 ίϯςφ #FBO #FBO #FBO #FBO #FBO ˞ʮΠϯελϯεʯͱ͸ɺ 
 ɹ΋ͪΖΜ+BWBͷΠϯελϯεͷ 
 ɹ͜ͱͰ͢ ඞཁʹԠͯ͡ 
 औΓग़ͯ͠࢖͏
  7. (C) CASAREAL, Inc. All rights reserved. KTVH ίϯςφ͸શ4QSJOHϓϩμΫτͷϕʔε ▸ ྫɿ4QSJOH.7$͸DispatcherServlet಺ʹ

    
 ɹɹίϯςφΛ͍࣋ͬͯΔ 7 DispatcherServlet ίϯςφ 7 ඞཁʹԠͯ͡ 
 औΓग़ͯ͠࢖͏ View 
 Resolver Handler 
 Mapping ɾɾɾ
  8. (C) CASAREAL, Inc. All rights reserved. KTVH ίϯςφͷػೳ ৄࡉ͸ޙड़ ᶃ

    %FQFOEFODZ*OKFDUJPO ᶄ είʔϓͷ؅ཧ ᶅ ϓϩΩγͷ࡞੒ 8
  9. (C) CASAREAL, Inc. All rights reserved. KTVH #FBOΛఆٛ͢Δํ๏ ᶃ ίϯϙʔωϯτεΩϟϯ

    ᶄ +BWB$PO f i H ᶅ ؔ਺ܕ#FBOఆٛ ࠓճ͸ׂѪ  ᶆ 9.- ࠓճ͸ׂѪ 9 ͜ΕΒͷํ๏͸ɺશͯซ༻Մೳʂ
  10. (C) CASAREAL, Inc. All rights reserved. KTVH #FBOఆٛํ๏ᶃ 
 ίϯϙʔωϯτεΩϟϯ

    ▸ ίϯϙʔωϯτεΩϟϯ 
 ʹࢦఆ͞Εͨύοέʔδ͔Β@Component͕ 
 ɹ෇͍ͨΫϥεΛ୳͢ ▸ ݟ͚ͭͨΒΠϯελϯεԽˠίϯςφʹอଘ 10
  11. (C) CASAREAL, Inc. All rights reserved. KTVH ▸ ӈهͷΞϊςʔγϣϯΛ 


    Ϋϥεʹ෇͚Δͱɺ 
 @ComponentΛ෇͚ͨ 
 ͜ͱͱಉ͡ʹͳΔ ▸ Ϋϥεͷ໾ׂʹԠͯ͡ 
 ม͑Δ 11 @Repository @Service @Controller @RestController @Configuration @ControllerAdvice @RestControllerAdvice ɾɾɾ #FBOఆٛํ๏ᶃ 
 ίϯϙʔωϯτεΩϟϯ ˞͜ΕΒͷΞϊςʔγϣϯͷιʔείʔυΛಡΉͱɺ 
 ɹ@Component͕෇͍͍ͯΔ͜ͱ͕෼͔Γ·͢
  12. (C) CASAREAL, Inc. All rights reserved. KTVH #FBOఆٛํ๏ᶃ 
 ίϯϙʔωϯτεΩϟϯ

    12 package com.example; @Component public class Hoge { ... } @Configuration @ComponentScan(basePackages = "com.example") public class AppConfig {} ▸ #FBOͱ͍ͨ͠Ϋϥε ▸ ઃఆΫϥε +BWB$PO f i H @Component͕ 
 ෇͍ͨΫϥεΛ 
 ୳͢ύοέʔδ
  13. (C) CASAREAL, Inc. All rights reserved. KTVH #FBOఆٛํ๏ᶃ 
 ίϯϙʔωϯτεΩϟϯ

    ▸ ίϯϙʔωϯτεΩϟϯൣғ͸ɺbasePackagesͰ 
 ࢦఆͨ͠ύοέʔδͷαϒύοέʔδҎԼ΋ؚΉ 13 @Configuration @ComponentScan( basePackages = "com.example") public class AppConfig {} com example foo bar baz εΩϟϯൣғ
  14. (C) CASAREAL, Inc. All rights reserved. KTVH #FBOఆٛํ๏ᶃ 
 ίϯϙʔωϯτεΩϟϯ

    ▸ basePackagesΛࢦఆ͠ͳ͔ͬͨ৔߹ɺ 
 @ComponentScan͕෇͍ͨΫϥεͷύοέʔδ 
 ͕basePackagesͱͳΔ 14 package com.example.config; @Configuration @ComponentScan CBTF1BDLBHFTແ͠ public class AppConfig {} ͜Ε͕ basePackages ʹͳΔʂ
  15. (C) CASAREAL, Inc. All rights reserved. KTVH #FBOఆٛํ๏ᶄ+BWB$PO f i

    H ▸ +BWB$PO f i HΫϥεʹϝιουΛ࡞੒͠ɺ 
 @BeanΛ෇Ճ͢Δ 
 ˠϝιουͷ໭Γ஋͕#FBOʹͳΔ 15 !$PNQPOFOU4DBO͸ෆཁ @Configuration public class AppConfig { @Bean public Hoge hoge() { return new Hoge(); } } !$PNQPOFOU͸ෆཁ public class Hoge { ... }
  16. (C) CASAREAL, Inc. All rights reserved. KTVH ίϯςφΛ࡞Δɾ#FBOΛऔಘ͢Δ ▸ +BWB$PO

    f i HΫϥεΛࢦఆͯ͠ 
 ApplicationContext ίϯςφ Λ࡞੒ ▸ ίϯϙʔωϯτεΩϟϯɾ+BWB$PO f i HͲͪΒͰ΋ڞ௨ ▸ getBean()Ͱ#FBOΛऔಘͰ͖Δ 16 ίϯςφͷ࡞੒ ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); #FBOΛ%*ίϯςφ͔ΒऔಘʢҾ਺͸ཉ͍͠#FBOͷσʔλܕʣ Hoge hoge = context.getBean(Hoge.class);
  17. (C) CASAREAL, Inc. All rights reserved. KTVH ίϯςφ࡞੒ͰߦΘΕΔ͜ͱ ▸ ίϯϙʔωϯτεΩϟϯͷ৔߹

     @Component͕෇͍ͨΫϥεΛ୳͢  ݟ͚ͭͨΒΠϯελϯεԽ  ίϯςφʹอଘ ▸ +BWB$PO f i Hͷ৔߹  @BeanϝιουΛ࣮ߦ  ໭Γ஋Λίϯςφʹอଘ 17 ίϯςφ࡞੒͸ 
 ॏ͍ॲཧͳͷͰɺ 
 ౓ίϯςφΛ 
 ࡞੒ͨ͠Β 
 ͦΕΛ࢖͍ճ͢ʂ ˞࣮ࡍʹ͸ɺΠϯελϯεੜ੒ͷλΠϛϯά͸ 
 ɹείʔϓʢޙड़ʣʹΑͬͯҟͳΓ·͢ɻ 
 ɹ͜͜Ͱઆ໌͍ͯ͠Δͷ͸singletonείʔϓͷ৔߹Ͱ͢
  18. (C) CASAREAL, Inc. All rights reserved. KTVH 4QSJOH#PPUͷ৔߹ 18 package

    com.example; @SpringBootApplication public class MyApplication { public static void main(String[] args) { ίϯςφͷ࡞੒ ApplicationContext context = SpringApplication.run(MyApplication.class); } } ͜Ε͕basePackages @Configuration @ComponentScan @EnableAutoConfiguration Λ૊Έ߹ΘͤͨΞϊςʔγϣϯ ˞@EnableAutoConfigurationʹ͍ͭͯ͸ԼهࢿྉΛࢀর 
 ɹIUUQTXXXTMJEFTIBSFOFUNBTBUPTIJUBEBTQSJOHCPPUKKVH
  19. (C) CASAREAL, Inc. All rights reserved. KTVH <͓·͚>#FBO*%ͱ͸ ▸ ֤#FBOΛࣝผ͢ΔͨΊͷ*%

    ▸ #FBOऔಘͳͲʹ࢖͏ʢۙ೥͸໓ଟʹ࢖Θͳ͍ʣ 
 ▸ !$PNQPOFOUͰ#FBOఆٛͨ͠৔߹ɺ 
 ʮΫϥε໊ͷ಄จࣈΛখจࣈʹͨ͠΋ͷʯ͕#FBO*% ▸ !#FBOͰ#FBOఆٛͨ͠৔߹ɺϝιου໊͕#FBO*% 19 Hoge hoge = (Hoge) context.getBean("Bean ID");
  20. (C) CASAREAL, Inc. All rights reserved. KTVH <͓·͚>#FBO*%ͱ͸ 20 @Component

    public class FooBar { ... } @Component("foo_bar") public class FooBar { ... } @Configuration public class FooConfig { @Bean public FooBar fooBar() { ... } @Bean(name = "foo_bar") public FooBar fooBar2() { ... } } fooBar foo_bar fooBar foo_bar #FBO*%
  21. (C) CASAREAL, Inc. All rights reserved. KTVH ίϯςφͷ·ͱΊ ▸ ίϯςφʹΠϯελϯε

    #FBO ͷೖΕ෺ ▸ ίϯϙʔωϯτεΩϟϯ ▸ @ComponentͰ#FBOʹ͢ΔΫϥεΛࢦఆɺ @ComponentScanͰ୳͢ൣғΛࢦఆ ▸ +BWB$PO f i H ▸ @BeanΛ෇Ճͨ͠ϝιουͷ໭Γ஋͕#FBOʹͳΔ 21
  22. (C) CASAREAL, Inc. All rights reserved. KTVH ໨࣍ ᶃ 4QSJOHͷίϯςφͱ͸

    ᶄ %*ͱ͸ ᶅ είʔϓͱ͸ ᶆ ϓϩΩγͱ͸ ᶇ "01ͱ͸ 22
  23. (C) CASAREAL, Inc. All rights reserved. KTVH ίϯςφͷػೳ ࠶ܝ ᶃ

    %FQFOEFODZ*OKFDUJPO ᶄ είʔϓͷ؅ཧ ᶅ ϓϩΩγͷ࡞੒ 23
  24. (C) CASAREAL, Inc. All rights reserved. KTVH %FQFOEFODZ*OKFDUJPOͱ͸ ▸ ೔ຊޠͰݴ͏ͱ

    24 ґଘੑͷ஫ೖ
  25. (C) CASAREAL, Inc. All rights reserved. KTVH 25

  26. (C) CASAREAL, Inc. All rights reserved. KTVH %FQFOEFODZ*OKFDUJPOͱ͸ 26 ґଘੑͷ஫ೖ

    ඞཁͳΠϯελϯε ࣗಈత ୅ೖ
  27. (C) CASAREAL, Inc. All rights reserved. KTVH OFX͠ͳ͍Ͱ୅ೖͯ͠΋Β͏ʹ%* 27 PCK"

    PCK# PCK" PCK# ୅ೖ OFX ⭕ ❌ ▸ %*͸ɺ#FBOੜ੒࣌ʹίϯςφ͕ߦ͏ ▸ OFX͠ͳ͍͜ͱͰɺ୯ମςετͳͲͷ࣌ʹ 
 Πϯελϯεͷࠩ͠ସ͕͑Մೳ
  28. (C) CASAREAL, Inc. All rights reserved. KTVH ΞϊςʔγϣϯʴίϯϙʔωϯτεΩϟϯͷ৔߹ ▸ ίϯετϥΫλʹ@AutowiredΛ෇Ճ͠ɺ

    
 Ҿ਺ʹཉ͍͠#FBOΛࢦఆ ▸ ίϯςφ͕ద੾ͳ#FBOΛҾ਺ʹ୅ೖͯ͘͠ΕΔ 28 @Component public class Hoge { private final Fuga fuga; @Autowired public Hoge(Fuga fuga) { this.fuga = fuga; } } ίϯςφ Hoge Fuga %* %* ˞Ϋϥε಺ʹίϯετϥΫλ͕ͭͷΈͷ৔߹ɺ 
 ɹ@Autowired͸লུՄೳ
  29. (C) CASAREAL, Inc. All rights reserved. KTVH !"VUPXJSFEͷলུ ▸ Ϋϥε಺ʹίϯετϥΫλ͕ͭͷΈͷ৔߹ɺ

    
 @AutowiredΛলུՄೳ 29 @Component public class Hoge { private final Fuga fuga; public Hoge(Fuga fuga) { this.fuga = fuga; } } @Autowired 
 লུՄೳ
  30. (C) CASAREAL, Inc. All rights reserved. KTVH +BWB$PO f i

    Hͷ৔߹ ▸ @BeanϝιουͷҾ਺ʹཉ͍͠#FBOΛࢦఆ͠ɺ 
 ϝιου಺ʹ୅ೖ͢ΔίʔυΛهड़ ▸ ίϯςφ͕ద੾ͳ#FBOΛҾ਺ʹ୅ೖͯ͘͠ΕΔ 30 @Configuration public class AppConfig { @Bean public Hoge hoge(Fuga fuga) { return new Hoge(fuga); } } ίϯςφ Hoge Fuga %* %*
  31. (C) CASAREAL, Inc. All rights reserved. KTVH ͍ΖΜͳ%* ▸ ίϯετϥΫλΠϯδΣΫγϣϯ

    31 @Component public class Hoge { private final Fuga fuga; @Autowired public Hoge(Fuga fuga) { this.fuga = fuga; } } @Component public class Hoge { private Fuga fuga; @Autowired public setFuga(Fuga fuga) { this.fuga = fuga; } } @Component public class Hoge { @Autowired Fuga fuga; } ▸ TFUUFSΠϯδΣΫγϣϯ ▸ ϑΟʔϧυΠϯδΣΫγϣϯ །ҰHogeΛมߋෆೳʹ Ͱ͖ΔͷͰ ίϯετϥΫλ ΠϯδΣΫγϣϯਪ঑ ˞ϝιουΠϯδΣΫγϣϯͱ΋ݺͼ·͢ ˞มߋෆೳ Πϛϡʔλϒϧ ʹϑΟʔϧυͷ஋Λॻ׵ෆՄೳ
  32. (C) CASAREAL, Inc. All rights reserved. KTVH %*ͷ·ͱΊ ▸ %*ʹඞཁͳΠϯελϯεͷ୅ೖ

    ▸ %*͸ίϯςφ͕#FBOΛੜ੒͢Δࡍʹߦ͏ ▸ ίϯετϥΫλʹҾ਺Λ෇͚ͯ@Autowired 
 PS@BeanϝιουʹҾ਺Ͱ#FBOΛ୅ೖՄೳ ▸ ίϯετϥΫλΠϯδΣΫγϣϯ͕ਪ঑ 32
  33. (C) CASAREAL, Inc. All rights reserved. KTVH ໨࣍ ᶃ 4QSJOHͷίϯςφͱ͸

    ᶄ %*ͱ͸ ᶅ είʔϓͱ͸ ᶆ ϓϩΩγͱ͸ ᶇ "01ͱ͸ 33
  34. (C) CASAREAL, Inc. All rights reserved. KTVH ίϯςφͷػೳ ࠶ܝ ᶃ

    %FQFOEFODZ*OKFDUJPO ᶄ είʔϓͷ؅ཧ ᶅ ϓϩΩγͷ࡞੒ 34
  35. (C) CASAREAL, Inc. All rights reserved. KTVH είʔϓͱ͸ ▸ ίϯςφ͕؅ཧ͍ͯ͠Δɺ#FBOͷ༗ޮൣғ

    ▸ #FBO͕͍ͭੜ੒͞Εɺ͍ͭഁغ͞ΕΔ͔ 35 είʔϓͷछྨ આ໌ TJOHMFUPO Πϯελϯε͸ͭͷΈʢσϑΥϧτʣ QSPUPUZQF ඞཁͳ࣌ʹΠϯελϯε͕ຖճ࡞ΒΕΔ SFRVFTU ϦΫΤετͱಉ͡ TFTTJPO ηογϣϯͱಉ͡
  36. (C) CASAREAL, Inc. All rights reserved. KTVH σϑΥϧτ͸TJOHMFUPO ▸ ͭͷΠϯελϯε͕

    
 ֤ॴͰ࢖͍ճ͞ΕΔ ▸ ϑΟʔϧυͰ஋Λ 
 อ࣋͢Δͷ͸ݫېʂʂ ▸ ηΩϡϦςΟࣄނʹʜ 36 #FBO ஋ " # ஋Λ 
 อଘ ஋͕ 
 ݟ͑Δ
  37. (C) CASAREAL, Inc. All rights reserved. KTVH είʔϓͷߟ͑ํ ▸ ΄ͱΜͲͷ#FBO͸TJOHMFUPOͰ0,

    ▸ ͦΕҎ֎ʹͨ͘͠ͳͬͨΒɺ·ͣઃܭΛݟ௚͢ ▸ ͨ·ʹTFTTJPO࢖͏͔ͳɾɾɾʁ͘Β͍ɻ 37
  38. (C) CASAREAL, Inc. All rights reserved. KTVH είʔϓͷࢦఆํ๏ᶃ ▸ @Scope("είʔϓ໊")Λ#FBOʹ෇Ճ

    38 @Scope("session") @Component public class Hoge { ... } @Configuration public class AppConfig { @Scope("request") @Bean public Hoge hoge() { ... } }
  39. (C) CASAREAL, Inc. All rights reserved. KTVH είʔϓͷࢦఆํ๏ᶄ ▸ @RequestScopeɾ@SessionScope΋༻ҙ͞Ε͍ͯΔ

    39 @SessionScope @Component public class Hoge { ... } @Configuration public class AppConfig { @RequestScope @Bean public Hoge hoge() { ... } }
  40. (C) CASAREAL, Inc. All rights reserved. KTVH είʔϓͷ·ͱΊ ▸ TJOHMFUPOɾQSPUPUZQFɾSFRVFTUɾTFTTJPOͷ

    
 छྨ ▸ σϑΥϧτ͸TJOHMFUPOɻ஋ͷอ࣋ݫېʂ ▸ @ScopeͰࢦఆ 40
  41. (C) CASAREAL, Inc. All rights reserved. KTVH ໨࣍ ᶃ 4QSJOHͷίϯςφͱ͸

    ᶄ %*ͱ͸ ᶅ είʔϓͱ͸ ᶆ ϓϩΩγͱ͸ ᶇ "01ͱ͸ 41
  42. (C) CASAREAL, Inc. All rights reserved. KTVH ίϯςφͷػೳ ࠶ܝ ᶃ

    %FQFOEFODZ*OKFDUJPO ᶄ είʔϓͷ؅ཧ ᶅ ϓϩΩγͷ࡞੒ 42
  43. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγ ϓϩΩγͱ͸ ▸

    ຊདྷ#FBOͱͳΔ͸ͣͩͬͨ ΠϯελϯεΛ 
 ϥοϓͨ͠#FBO ▸ ͢΂ͯͷ#FBOʹϓϩΩγ͕ ෇͘Θ͚Ͱ͸ͳ͍ʢޙड़ʣ ▸ ᶃΠϯλϑΣʔε·ͨ͸ 
 ᶄܧঝ 
 Λར༻ͯ͠࡞ΒΕΔ 43 <<interface>> Hoge HogeImpl
  44. (C) CASAREAL, Inc. All rights reserved. KTVH 44

  45. (C) CASAREAL, Inc. All rights reserved. KTVH Ͳ͏͍͏͜ͱʁ 45 @Component

    public class HogeImpl implements Hoge { public void doSomething() { ... } } // ͜ͷίʔυ͸ΠϝʔδͰ͢ public class HogeProxy implements Hoge { @Override public void doSomething() { ... } } ͜Ε͕#FBOʹͳΔͷ 
 Ͱ͸ͳ͘ɾɾɾ ͜ΜͳΫϥε͕ 
 ࣮ߦ࣌ʹੜ੒͞Εɺ 
 ͜Ε͕#FBOʹͳΔʂ
  46. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγͷ໨తᶃ"01 ▸ ຊདྷͷॲཧͷલޙ

    ʹɺׂΓࠐΈॲཧ Λߦ͏ ▸ τϥϯβΫγϣϯͷ ։࢝ɾऴྃɺ 
 ݖݶνΣοΫͳͲ ▸ ৄࡉ͸ޙड़ 46 ͜ͷίʔυ͸ΠϝʔδͰ͢ public class HogeProxy implements Hoge { public void doSomething() { ຊདྷͷΠϯελϯε Hoge hoge = ...; લॲཧͷׂΓࠐΈ interceptor.doBefore(); ຊདྷͷॲཧ hoge.doSomething(); ޙॲཧͷׂΓࠐΈ interceptor.doAfter(); } }
  47. (C) CASAREAL, Inc. All rights reserved. KTVH @Scope("singleton") @Component public

    class SingletonScopeBeanImpl implements SingletonScopeBean { private final RequestScopeBean rsb; @Autowired public SingletonScopeBean(RequestScopeBean rsb) { this.rsb = rsb; } public void doSomething() { rsb.execute(); } } ϓϩΩγͷ໨తᶄείʔϓͷҧ͍Λٵऩ 47 ͪ͜Β΋TJOHMFUPOʹ ͳͬͯ͠·͏ʁ 
 ˠ%*͞ΕΔͷ͸ ϓϩΩγ
  48. (C) CASAREAL, Inc. All rights reserved. KTVH ͜ͷίʔυ͸ΠϝʔδͰ͢ public class

    RequestScopeBeanProxy implements RequestScopeBean { ApplicationContext context; @Override public void execute() { RequestScopeBean rsb = context.getBean(RequestScopeBean.class); rsb.execute(); } } ϓϩΩγͷ໨తᶄείʔϓͷҧ͍Λٵऩ 48 ˞͜ͷϓϩΩγ͸ΠϯλϑΣʔεͰ࡞੒͍ͯ͠·͕͢ɺ 
 ɹܧঝͷ৔߹Ͱ΋ಉ༷Ͱ͢ ຖճίϯςφ͔Β औಘ͍ͯ͠Δʂ
  49. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγͷ໨తᶅ#FBOΛγϯάϧτϯʹอͭ 49 @Configuration

    public class HogeConfig { @Bean public Sample1 sample1() { return new Sample1(); } @Bean public Sample2 sample2() { return new Sample2(sample1()); } @Bean public Sample3 sample3() { return new Sample3(sample1()); } } 4BNQMFͷΠϯελϯε͕ ͭ࡞ΒΕΔʂʁ 4BNQMFͷΠϯελϯε͕ 
 ͭ࡞ΒΕΔʂʁ 
 γϯάϧτϯͰͳ͘ͳΔʂʁ
  50. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγͷ໨తᶅ#FBOΛγϯάϧτϯʹอͭ 50 //

    ͜ͷίʔυ͸ΠϝʔδͰ͢ public class HogeConfigProxy extends HogeConfig { @Override public Sample1 sample1() { if (طʹDIίϯςφʹSample1ͷBean͕͋ͬͨΒ) { return context.getBean(Sample1.class) } else { return new Sample1(); } } ... } TBNQMF ͕Կճ࣮ߦ͞Εͯ΋ 
 #FBO͸γϯάϧτϯʂ
  51. (C) CASAREAL, Inc. All rights reserved. KTVH ᶃ "01Λར༻͍ͯ͠Δ#FBO ᶄ

    @ScopeͰproxyMode=TARGET_CLASS 
 ·ͨ͸INTERFACES͕ࢦఆ͞Ε͍ͯΔ#FBO ᶅ @ComponentScanʹscopedProxy=TARGET_CLASS 
 ·ͨ͸INTERFACES͕ࢦఆ͞Ε͍ͯΔ#FBO ᶆ @ConfigurationʹproxyBeanMethods=true 
 ͕ࢦఆ͞Ε͍ͯΔ+BWB$PO f i HʢσϑΥϧτͰtrueʣ ͲΜͳ#FBOʹϓϩΩγ͕࡞ΒΕΔ͔ 51 5"3(&5@$-"44ˠ$(-*# 
 */5&3'"$&4ˠ+%,
  52. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγͷ࡞ΒΕํᶃ+%,1SPYZ ▸ σϑΥϧτͷڍಈ

    ▸ ຊདྷͷΫϥε͕࣮૷͍ͯ͠Δશͯͷ 
 ΠϯλϑΣʔεΛ࣮૷ͨ͠Ϋϥε͕ 
 ࣮ߦ࣌ʹ࡞ΒΕɺ 
 ͦΕ͕ϓϩΩγʢ͔ͭ#FBOʣͱͳΔ ▸ java.lang.reflect.Proxyͷػೳ ▸ ԿΒ͔ͷΠϯλϑΣʔεΛ࣮૷ͨ͠ 
 Ϋϥεʹ࢖ΘΕΔ 52 ϓϩΩγ <<interface>> Hoge HogeImpl ࣮૷
  53. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγͷ࡞ΒΕํᶄ$(-*#1PYZ ▸ ຊདྷͷΫϥεͷαϒΫϥε͕

    
 ࣮ߦ࣌ʹ࡞ΒΕɺ 
 ͦΕ͕ϓϩΩγʢ͔ͭ#FBOʣͱ ͳΔ ▸ 4QSJOH͕಺แ͍ͯ͠Δ 
 ϥΠϒϥϦʮ$(-*#ʯͷػೳ ▸ ԿͷΠϯλϑΣʔε΋ 
 ࣮૷ͯ͠ͳ͍Ϋϥεʹ࢖ΘΕΔ 53 ϓϩΩγ HogeImpl HogeImpl ܧঝ
  54. (C) CASAREAL, Inc. All rights reserved. KTVH #FBO͕ΠϯλϑΣʔεΛ࣮૷͍ͯͯ͠΋ 
 $(-*#1SPYZΛ࢖͍͍ͨ৔߹

    ᶃ 4QSJOH#PPUͰ͸σϑΥϧτ͕$(-*#1SPYZ ▸ spring.aop.proxy-target-class=trueͰ͋ΔͨΊ 
 ʢfalseʹ͢Ε͹+%,1SPYZʣ ᶄ @Scope(proxyMode=TARGET_CLASS) ▸ ෇Ճͨ͠#FBOͷΈ͕ର৅ ᶅ @ComponentScan(scopedProxy=TARGET_CLASS) ▸ ίϯϙʔωϯτεΩϟϯͨ͠શBean͕ର৅ 54
  55. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγͷ੍໿ ▸ +%,1SPYZ

    ▸ গͳ͘ͱ΋ͭͷΠϯλϑΣʔεΛ࣮૷͠ͳ͚Ε͹ 
 ͳΒͳ͍ ▸ $(-*#1SPYZ ▸ Ϋϥε΍ϝιουΛ f i OBMʹͯ͠͸ͳΒͳ͍ 55
  56. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγͷ·ͱΊ ▸ ϓϩΩγʹຊདྷ#FBOͱͳΔ͸ͣͩͬͨΠϯελϯεΛ

    
 ɹɹɹɹɹϥοϓͨ͠#FBO ▸ ΠϯλϑΣʔεʢ+%,1SPYZʣ·ͨ͸ 
 ܧঝʢ$(-*#1SPYZʣΛར༻ͯ͠࡞ΒΕΔ ▸ 4QSJOH#PPUͰ͸σϑΥϧτ͕$(-*#1SPYZ ▸ "01ɺείʔϓͷҧ͍ͷٵऩɺ#FBOΛγϯάϧτϯʹอͭɺ 
 ͕໨త ▸ ֤ϓϩΩγͷ੍໿ʹ஫ҙ 56
  57. (C) CASAREAL, Inc. All rights reserved. KTVH ໨࣍ ᶃ 4QSJOHͷίϯςφͱ͸

    ᶄ %*ͱ͸ ᶅ είʔϓͱ͸ ᶆ ϓϩΩγͱ͸ ᶇ "01ͱ͸ 57
  58. (C) CASAREAL, Inc. All rights reserved. KTVH "01ͱ͸ 58 ▸

    ຊདྷͷॲཧͷલޙʹɺׂΓࠐΈॲཧΛߦ͏ ▸ τϥϯβΫγϣϯͷ։࢝ɾऴྃɺݖݶνΣοΫͳͲ
  59. (C) CASAREAL, Inc. All rights reserved. KTVH "01ͷ࢖͍ํ 59 @Configuration

    @EnableAspectJAutoProxy public class AspectConfig { ... } 4QSJOH#PPUͰ͸ෆཁ
  60. (C) CASAREAL, Inc. All rights reserved. KTVH "01ͷ࢖͍ํ 60 @Aspect

    @Component public class LoggingAspect { @Around("execution(* com.example.*.*(..))") public Object log(ProceedingJoinPoint jp) throws Exception { System.out.println("start"); // લॲཧ Object value = jp.proceed(); // ຊདྷͷॲཧ System.out.println("end"); // ޙॲཧ return value; } } DPNFYBNQMFύοέʔδͷ 
 શΫϥεɾશϝιουͷલޙʹׂΓࠐΉ
  61. (C) CASAREAL, Inc. All rights reserved. KTVH "01ͷ࢖͍ํ 61 package

    com.example; @Component public class HelloService { public void hello() { System.out.println("hello"); } } ׂΓࠐΈର৅͸ 
 ඞͣ#FBO start hello end ࣮ߦ݁Ռ
  62. (C) CASAREAL, Inc. All rights reserved. KTVH ׂΓࠐΊΔ৔ॴ ▸ ϝιου։࢝௚લ

    
 ˠ@Before ▸ ϝιουऴྃ௚ޙ 
 ˠ@AfterReturningɾ@AfterThrowing 
 ɾ@After ▸ ϝιου։࢝௚લɾऴྃ௚ޙͷ྆ํ 
 ˠ@Around 62
  63. (C) CASAREAL, Inc. All rights reserved. KTVH ϙΠϯτΧοτࣜ ▸ ͲͷΫϥεɾͲͷϝιουʹׂΓࠐΉ͔ͷ

    
 ৚݅Λࢦఆ͢Δ 63 execution(* com.example.*.*(..)) execution(@annotation(com.example.MyAnno)) ໭Γ஋ͷ 
 ܕ͸೚ҙ DPNFYBNQMFύοέʔδͷ 
 શΫϥεɾશϝιου Ҿ਺ͷݸ਺͸ 
 ೚ҙ @com.example.MyAnnoΞϊςʔγϣϯ͕ 
 ෇Ճ͞Εͨશϝιου ϙΠϯτΧοτͷਖ਼ࣜͳจ๏͸ɺAspectJͷϦϑΝϨϯεΛࢀর 
 https://www.eclipse.org/aspectj/doc/next/progguide/language-joinPoints.html
  64. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγʹΑΔ"01ͷ࣮ݱ 64 ϓϩΩγ

    HelloService HelloService Impl Interceptor ▸ ϓϩΩγ಺ʹɺׂΓࠐΈॲཧΛ࣮ߦ͢Δ *OUFSDFQUPS͕ಉډ͢Δ
  65. (C) CASAREAL, Inc. All rights reserved. KTVH ϓϩΩγʹΑΔ"01ͷ࣮ݱ 65 ͜ͷίʔυ͸ΠϝʔδͰ͢

    public class HelloServiceProxy implements HelloService { @Override public void hello() { HelloService target = ...; // ׂΓࠐΈର৅ Interceptor interceptor = ...; // ׂΓࠐΈॲཧ୲౰ interceptor.doBefore(); // લॲཧ target.hello(); // ຊདྷͷॲཧ interceptor.doAfter(); // ޙॲཧ } }
  66. (C) CASAREAL, Inc. All rights reserved. KTVH "01ͷ஫ҙ఺ ▸ ಉҰΫϥε಺ͷϝιουݺͼग़͠ʹ͸ׂΓࠐΈ͕͞Εͳ͍ʂ

    ▸ ಛʹ!5SBOTBDUJPOBMͰؒҧ͍ଟ͠ 66 @Component public class SampleService { public void method1() { System.out.println("method1()"); method2(); } public void method2() { System.out.println("method2()"); } } ͜ͷmethod2()ʹ͸ 
 ׂΓࠐΈ͞Εͳ͍ʂ
  67. (C) CASAREAL, Inc. All rights reserved. KTVH ͳׂͥΓࠐΈ͞Εͳ͍ͷ͔ 67 ಉҰΫϥε಺͔ͩΒ

    
 *OUFSDFQUPS͕࣮ߦ͞Εͳ͍ʂ
  68. (C) CASAREAL, Inc. All rights reserved. KTVH ׂΓࠐΈ͞ΕΔΑ͏ʹ͢Δʹ͸ 68 @Configuration

    @EnableAspectJAutoProxy(exposeProxy = true) public class AspectConfig { ... } @Component public class SampleService { public void method1() { ... ((SampleService) AopContext.currentProxy()) .method2(); } public void method2() { ... } } 4QSJOH#PPUͰ΋෇͚Δ
  69. (C) CASAREAL, Inc. All rights reserved. KTVH "01ͷ·ͱΊ ▸ @AroundͳͲͷΞϊςʔγϣϯͱɺ

    
 ϙΠϯτΧοτࣜͰׂΓࠐΈର৅Λࢦఆ͢Δ ▸ "01͸ϓϩΩγʹΑ࣮ͬͯݱ͞Ε͍ͯΔ ▸ ಉҰΫϥε಺ͷϝιουݺͼग़͠ʹ͸ 
 ׂΓࠐΈ͞Εͳ͍ͷͰ஫ҙ 69
  70. (C) CASAREAL, Inc. All rights reserved. KTVH ·ͱΊ 70

  71. (C) CASAREAL, Inc. All rights reserved. KTVH ຊ೔ͷ·ͱΊ ▸ 4QSJOH͸ίϯςφͰ#FBOΛ؅ཧ͍ͯ͠Δ

    ▸ %*ͱ͸ɺඞཁͳ#FBOͷࣗಈత୅ೖ ▸ είʔϓ͸̐छྨɻσϑΥϧτ͸TJOHMFUPO ▸ #FBOʹ͸ϓϩΩγ͕࡞ΒΕ͍ͯΔ৔߹͕͋Δ ▸ "01͸ϓϩΩγʹΑ࣮ͬͯݱ͞Ε͍ͯΔ 71
  72. (C) CASAREAL, Inc. All rights reserved. KTVH ͓͢͢Ίࢢൢॻ੶ ▸ ্هҎ֎͸ɺ಺༰ͷ৴པੑ͕ඍົͳͷͰɺ

    
 ݸਓతʹ͸͓͢͢Ί͠·ͤΜ 72
  73. (C) CASAREAL, Inc. All rights reserved. KTVH ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ 73