Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

More Maintainable, Testable Applications with Dependency Injection in Spring

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Nächste SlideShare
Tdd iPhone For Dummies
Tdd iPhone For Dummies
Wird geladen in …3
×

Hier ansehen

1 von 36 Anzeige

More Maintainable, Testable Applications with Dependency Injection in Spring

Herunterladen, um offline zu lesen

WaffleCorp is a .NET shop, whilst Waverley’s Waffles utilizes the power of Spring with Java. Now developers from both tech stacks are coming together as a team.

Layla is a .NET developer and has no idea of all the great and useful things that Spring has to offer. Jakub will be sharing his knowledge of Spring and dependency injection, covering IoC, Spring core, Spring tests, Spring Boot, and more. Through a collection of live demos, Jakub and Layla will guide you through some Spring fundamentals, why they're important, and when to use them (and when not to).

WaffleCorp is a .NET shop, whilst Waverley’s Waffles utilizes the power of Spring with Java. Now developers from both tech stacks are coming together as a team.

Layla is a .NET developer and has no idea of all the great and useful things that Spring has to offer. Jakub will be sharing his knowledge of Spring and dependency injection, covering IoC, Spring core, Spring tests, Spring Boot, and more. Through a collection of live demos, Jakub and Layla will guide you through some Spring fundamentals, why they're important, and when to use them (and when not to).

Anzeige
Anzeige

Weitere Verwandte Inhalte

Ähnlich wie More Maintainable, Testable Applications with Dependency Injection in Spring (20)

Weitere von VMware Tanzu (20)

Anzeige

Aktuellste (20)

More Maintainable, Testable Applications with Dependency Injection in Spring

  1. 1. More Maintainable, Testable Applications with Dependency Injection with Spring
  2. 2. INTRODUCTION Maintainable and testable code is always desirable, but not always easy to write. Spring and Spring Boot solve this, let's see how!
  3. 3. Plain Java No separation of concerns (SoC), not testable 01 Service Locator Pattern A common pattern for improving testability 02 Conclusions What now? 04 IoC & Spring How do Spring and Spring Boot make life easier? 03
  4. 4. Plain Java 01
  5. 5. Go to code!
  6. 6. The WaffleCreator `prepare` method has a dependency on `MacronutrientsProvider` Production Code
  7. 7. Production Code prepare(...) { } MacronutrientsProvider mp = new MacronutrientsProvider(...) //.. mp.fetch() fetch(...) { } new(...) { } HTTP call WaffleCreator MacronutrientsProvider
  8. 8. Testing this Code prepare(...) { } MacronutrientsProvider mp = new MacronutrientsProvider(...) //.. mp.fetch() fetch(...) { } new(...) { } HTTP call WaffleCreator MacronutrientsProvider We have no control over the HTTP call
  9. 9. Not testable We can't control the dependencies.
  10. 10. At the moment, there is also no easy way to have different implementations for different use-cases. So what can we do?
  11. 11. Service Locator Pattern 02
  12. 12. Service Locator get(...) { } put(...) { } MacronutrientsProvider MacronutrientsProviderLocator MacronutrientsProvider fetch(...) { } new(...) { } HTTP call
  13. 13. Service Locator get(...) { } put(...) { } MacronutrientsProvider MacronutrientsProviderLocator MacronutrientsProvider fetch(...) { } new(...) { } Stubbedcall
  14. 14. WaffleCreator prepare(...) { } MacronutrientsProvider mp = MacronutrientsProviderLocator.get(...); //.. mp.fetch() Config main(...) { } MacronutrientsProviderLocator .put(new MacronutrientsProvider(...)); Using the Service Locator
  15. 15. WaffleCreator prepare(...) { } MacronutrientsProvider mp = MacronutrientsProviderLocator.get(...); //.. mp.fetch() WaffleCreatorTest init(...) { } MacronutrientsProviderLocator.put(mock(MacronutrientsProvider)); Testing the Service Locator
  16. 16. Go to code!
  17. 17. Extendable! We can add multiple service providers without modifying code.
  18. 18. Testable! We can stub or mock the dependencies.
  19. 19. Stub vs Mock Stub Mock Taught how to behave Verified with expectations
  20. 20. Generic Service Locator get(...) { } put(...) { } ServiceLocator Some Other Services Multiple implementations
  21. 21. Go to code!
  22. 22. WaffleCreator prepare(...) { } MacronutrientsProvider mp = ServiceLocator.get(MacronutrientsProvider.class,“nosugar); mp.fetch(); Config init(...) { } ServiceLocator.put(“nosugar”, new MacronutrientsProvider(...), MacronutrientsProvider.class)); Using the Generic Service Locator
  23. 23. WaffleCreator prepare(...) { } MacronutrientsProvider mp = ServiceLocator.get(MacronutrientsProvider.class,“nosugar); mp.fetch(); Config init(...) { } ServiceLocator.put(“nosugar”, new MacronutrientsProvider(...), MacronutrientsProvider.class)); Using the Generic Service Locator
  24. 24. But now too much boilerplate code! ServiceLocator.get(HttpClient.class, "with timeouts"); ServiceLocator.get(MacronutrientsProvider.class, "high sugar"); ServiceLocator.get(WaffleDatabase.class, "mysql");
  25. 25. Single Point of Failure
  26. 26. Service Locator has many dependencies Service Locator
  27. 27. Inversion of Control
  28. 28. Inversion of Control options Dependency Injection Service Locator Events Aspects Strategy pattern via constructor via setter
  29. 29. Inversion of Control IoC Container
  30. 30. Spring Core 03
  31. 31. Beans
  32. 32. Go to code!
  33. 33. Key Annotations @Bean added when using java config @Configuration added for classes containing @Bean declarations @Component added when using component scan @Repository component that is a repository @Service component that is a business service @Qualifier mocks/stubs bean @MockBean specifies name of the injected bean @SpringBootTest automatically bootstraps IoC
  34. 34. Java Config Beans are not polluted with annotations Component Scan No need to add additional configuration classes XML Not used much nowadays Bean Registration There are three different ways that Spring can register dependencies.
  35. 35. Go to code!
  36. 36. CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik Thanks! Repo: github.com:pilloPl/waffles.git Slides: https://bit.ly/spring-di @LaylaCodesIt @JakubPilimon

×