A short presentation that explains the internals of the Spring Boot framework. Includes an example of a starter and an autoconfig artifac, and a github repo:
https://github.com/MichelSchudel/springbootunderdahood
6. SpringApplication.run(MyClass.class, args);
1. Creates an Spring applicationContext based on
1. the context information of the class passed in the runner (in this case,
MyClass.class)
2. Type of context (FE webappContext) based on classes in the classpath
2. Deduces the “main” class (for runnable jar packaging - > java –jar myApp.jar)
3. Enriches the applicationContext if AutoConfiguration annotation is present (more on this
in a moment)
4. Returns the applicationContext to the user / caller
12. AutoConfigurationImportSelector
• Loads configuration classes using each META-INF/spring.factories it can find,
using SpringFactoriesLoader class
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,
org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveRestClientAutoConfiguration,
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,
META-INF/spring.factories
15. How is the embedded webserver (Tomcat)
started?
…Just another AutoConfig!
• If applicationContext type is SERVLET (Servlet class is on the
classpath),
EmbeddedWebServerFactoryCustomizerAutoConfiguration
is loaded
• If Tomcat class is on the classpath,
TomcatWebServerFactoryCustomizer is initialized, which
configures and starts Tomcat server with a DispatcherServlet that
delegates requests to the applicationContext (normal SpringWebMvc
behaviour)
16. How to build your own autoconfig!
• SomeLibrary – existing library that you want to support in SB
• SomeLibraryAutoConfig – contains conditionals and bean factories
• SomeLibraryStarter – just a POM that bundles as dependencies:
• SomeLibrary
• SomeLibraryAutoConfig
• Your Application – imports SomeLibraryStarter as a dependency
17. Artifact dependency tree
Your SB Application
SomeLibrary
Starter
SomeLibrary
AutoConfiguration
SomeLibrary
Depends on
Depends on
Depends on
Depends on
Spring factories
@Conditional configs
Just a pom
FlyWay, H2
19. Summary –Spring Boot at its core
• Provide utility class to load application context
• Introduce autoconfig mechanism through @Conditional annotations
https://github.com/MichelSchudel/springbootunderdahood