Upgrade to Pro — share decks privately, control downloads, hide ads and more …

It's a kind of magic? - Custom Spring Boot Starter

It's a kind of magic? - Custom Spring Boot Starter

How does Spring Boots auto configuration works? Learn that there is by far no magic behind Spring Boots auto configuration.

You'll find the talks demo at https://github.com/michael-simons/springio2016.

The basic idea of the demo using a fun topic like banners to demonstrate features of Spring Boots auto configuration and show that all this stuff by far isn't magic.

Michael Simons

May 19, 2016
Tweet

More Decks by Michael Simons

Other Decks in Programming

Transcript

  1. It’s a kind of magic?
    Custom Spring Boot Starter
    Michael Simons
    @rotnroll666

    View full-size slide

  2. Spring I/O
    2016
    ABOUT ME
    MICHAEL SIMONS
    ▸ Doing Java for 14 years at 

    enerko-informatik.de
    ▸ Mainly database centric
    applications in utilities (electricity
    and gas)
    ▸ Runs the Aachen based „Euregio
    JUG“
    ▸ Blogs at info.michael-simons.eu
    ▸ Find me on twitter: @rotnroll666

    View full-size slide

  3. Spring I/O
    2016
    INTRODUCTION
    SPRING BOOT: GOALS
    ▸ A faster ‘getting started’ experience for all Spring
    development
    ▸ Opinionated out of the box with sensible defaults
    ▸ Get out of the way quickly as your requirements start to
    grow
    ▸ Provide a range of non-functional features
    ▸ Externalized configuration

    View full-size slide

  4. Spring I/O
    2016
    SPRING BOOT DOESN’T
    CONSIST OF ONE BIG
    ARTIFACT
    MODULARIZATION

    View full-size slide

  5. Spring I/O
    2016
    STARTING…WHAT?
    ALL KINDS OF SPRING BOOT STARTERS
    ▸ Integration (Databases, Caches)
    ▸ Template Engines
    ▸ Validation
    ▸ Social
    ▸ many more…

    https://github.com/spring-projects/spring-boot/tree/
    master/spring-boot-starters

    View full-size slide

  6. HTTP://START.SPRING.IO

    View full-size slide

  7. Spring I/O
    2016
    ▸ AUTOCONFIGURE MODULE
    ▸ SPRING.FACTORIES
    ▸ STARTER MODULE
    ANATOMY
    OF A STARTER

    View full-size slide

  8. Spring I/O
    2016
    THE AUTOCONFIGURE MODULE - A KIND OF MAGIC?
    AUTO CONFIGURATION
    ▸ Configuration through (Java) Code
    combined with @Conditional
    ▸ OnClassCondition / OnMissingClassCondition
    ▸ OnBeanCondition / OnMissingBeanCondition
    ▸ OnPropertyCondition
    ▸ OnResourceCondition
    ▸ OnExpressionCondition
    ▸ OnJavaCondition
    ▸ OnJndiCondition
    ▸ OnWebApplicationCondition

    View full-size slide

  9. Spring I/O
    2016
    DEMO
    THYMELEAF-BANNER-SPRING-BOOT-STARTER
    We will turn this:

    into

    View full-size slide

  10. Spring I/O
    2016
    RECAP
    BASIC AUTO CONFIGURATION
    ▸ @SpringBootApplication enables auto configuration
    ▸ spring.factories
    ▸ @ConditionalOnClass
    ▸ @AutoConfigureAfter and @AutoConfigureBefore
    ▸ @Bean

    View full-size slide

  11. Spring I/O
    2016
    RECAP
    MORE CONDITIONS AND ORDERING
    ▸ @ConditionalOnProperty
    ▸ @ConditionalOnMissingBean
    ▸ @Order

    View full-size slide

  12. Spring I/O
    2016
    RECAP
    AUTO CONFIGURATION REPORT
    ▸ Either through —debug parameter
    ▸ Adding

    spring-boot-starter-actuator 

    provides the REST endpoint

    /autoconfig

    View full-size slide

  13. Spring I/O
    2016
    RECAP
    CREATING CUSTOM CONDITIONS
    ▸ Implement o.s.c.annotation.Condition
    ▸ Extend o.s.boot.autoconfigure.SpringBootCondition
    ▸ Use one of
    ▸ AllNestedConditions
    ▸ AnyNestedCondition
    ▸ NoneNestedCondition

    View full-size slide

  14. Spring I/O
    2016
    BANNER ALL THE THINGS… AND MORE!
    CONCLUSION
    ▸ Provide features in a resilient way
    ▸ Custom modules can easily benefit from the starter
    mechanism
    ▸ No magic: Every automatically configuration can be
    explained
    ▸ @EnableAutoConfiguration w SpringFactoriesLoader

    provides the heavy lifting, there’s no full class path
    scanning or similar involved
    ▸ You can use it yourself!

    View full-size slide

  15. Spring I/O
    2016
    RESOURCES
    THANK YOU FOR LISTENING!
    ▸ This talk: github.com/michael-simons/springio2016
    ▸ A more useful starter of mine:

    wro4j-spring-boot-starter
    ▸ Contact: michael-simons.eu
    ▸ Twitter: @rotnroll666

    View full-size slide