Breaking the Magician's Code - Diving deeper into Spring Boot internals

Bd3e6a603ad7608262e025fabe5343b6?s=47 Phil Webb
February 28, 2017

Breaking the Magician's Code - Diving deeper into Spring Boot internals

Presented @DevNexus '17

Bd3e6a603ad7608262e025fabe5343b6?s=128

Phil Webb

February 28, 2017
Tweet

Transcript

  1. Breaking the Magician's Code Diving deeper into Spring Boot internals

  2. @phillip_webb @madhurabhave23

  3. None
  4. DEMO start.spring.io

  5. DEMO Starting from scratch

  6. Embedded Servlet Container Tomcat WAR ServletContainerInitializer AbstractAnnotationConfigDispatcherServletInitializer WebApplicationContext Dispatcher Servlet

  7. Embedded Servlet Container WebApplicationContext Embedded EmbeddedServletContainerFactory EmbeddedServletContainer getEmbeddedServletContainer( ServletContextInitializer... initializers);

    EmbeddedServletContainer start() stop() getPort()
  8. DEMO Back to the CODE

  9. Servlets WebApplicationContext Embedded Servlet Filter ServletContextInitializer ServletRegistrationBean FilterRegistrationBean

  10. Servlets WebApplicationContext Embedded Servlet DispatcherServlet @RequestMapping(“/foo”) @RequestMapping(“/bar”)

  11. DEMO Back to the CODE

  12. Component scanning • @Componentscan • base Packages • base Package

    Classes • (default)
  13. Component Scanning com.example.demo.core.* com.example.demo.domain.* com.example.demo.Application com.example.demo.config.*

  14. Component Scanning example demo com config core domain

  15. Component Scanning example demo com config core domain .Application

  16. Component Scanning example demo com config core domain .Application

  17. Component Scanning example demo com config core domain .Application

  18. Component Scanning example demo com config core domain .Application

  19. DEMO Back to the CODE

  20. Meta-Annotations • Most Spring Annotations can be used as meta-annotations

    • Use in your own code • Used extensively in the framework: •@RestController •@GetMapping
  21. META-Annotations • @AliasFor for advanced mappings: @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan public

    @interface SpringBootApplication { @AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude") Class<?>[] exclude() default {}; @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") String[] scanBasePackages() default {};
  22. DEMO Back to the CODE

  23. Advanced ImportS • @Import(Regular.class) • ImportSelector • Return String[] •

    ImportBeanDefinitionRegistrar • Direct registration
  24. Spring Factories • Dirt simple way to discover implementations •

    Similar to Java Service Loader • Simple properties file • /META-INF/spring.factories
  25. DEMO Back to the CODE

  26. Conditions • @Conditional annotation • Condition interface public interface Condition

    { boolean matches( ConditionContext context, AnnotatedTypeMetadata metadata); }
  27. Conditions • Spring Framework @Profile • Spring Boot (Bean,Class,Property) •

    Ordered so fastest run first
  28. DEMO Back to the CODE

  29. Spring Boot Code •ConditionalOnAClass -> ConditionalOnClass • MagicApplication -> SpringBootApplication

    • EnableMagicAutoConfiguration -> EnableAutoConfiguration • EnableMagicProperties -> EnableConfigurationProperties • *Configuration -> *AutoConfiguration
  30. Summary • There’s no such thing as magic • Follow

    the breadcrumbs • Annotations • Import Selectors • Spring Factories • Conditions • Use the source!
  31. Thanks! Questions? @madhurabhave23 @phillip_webb speakerdeck.com/philwebb https://github.com/mbhave/non-magic-demo