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

Master Spring Boot auto-configuration

Master Spring Boot auto-configuration

Sample app: https://github.com/snicoll-demos/spring-boot-master-auto-configuration

Are you wondering how Spring Boot is actually working in the inside? No black magic here, but standard mechanisms to register beans according to the environment. Spring Boot supports many use cases already and it can also be extended to support your own! In this talk we are going to create a starter step by step (dependencies management, auto-configuration management, customization through dedicated keys, testing, etc). You can take this home and apply that to your own components; this will bring standard auto-configuration for your components with the ability to reconfigure that default behaviour if need to be.

Stéphane Nicoll

April 15, 2015
Tweet

More Decks by Stéphane Nicoll

Other Decks in Programming

Transcript

  1. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Master Spring Boot auto-configuration Stéphane Nicoll Pivotal
  2. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ @snicoll 2 https://github.com/snicoll @snicoll Stéphane Nicoll [email protected]
  3. Demo Unless otherwise indicated, these slides are 
 © 2013-2014

    Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Auto-configuration
  4. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Auto-configuration class ! Regular @Configuration class ! Referenced in META-INF/spring.factories ! Enabled (Guarded) via certain conditions ! Can be triggered before or after another auto-configuration • @AutoConfigureBefore • @AutoConfigureAfter ! Displayed in the auto-configuration report • Logs when debug is enabled (—debug) • /autoconfig on a web app with the actuator 4 @snicoll
  5. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring 4 @Conditional 5 @snicoll @Configuration @Conditional(CustomCondition.class) public class AppConfiguration { // @Bean methods } public class CustomCondition implements Condition { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { ... } }
  6. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Core conditions ! @ConditionalOn[Missing]Class ! @ConditionalOn[Missing]Bean ! @ConditionalOnProperty ! @ConditionalOnResource ! @ConditionalOnExpression (SpEL) ! @ConditionalOn[Not]WebApplication ! @ConditionalOnJava - @ConditionalOnJndi ! AnyNestedCondition ! [your condition here] 6 @snicoll
  7. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Let’s build our own auto-config! ! xyz-spring-boot-autoconfigure • Auto-configuration classes and related code ! xyz-spring-boot-starter • Necessary dependencies to use feature xyz ! Can be combined in most cases ! Example based on HornetQ • Actually implemented in Spring Boot • Real world example with interesting configuration options 7 @snicoll
  8. Demo Unless otherwise indicated, these slides are 
 © 2013-2014

    Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Your auto-configuration
  9. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ @ConfigurationProperties ! Complete infrastructure to customize your auto-configuration ! Just add @EnableConfigurationProperties(MyProperties.class) • Add that class as a bean in the context (i.e. you can inject it) • Inject any key in the Environment that match the prefix + property name • Works with system property, command-line switch, key in configuration file, etc. ! Add the annotation processor to your build • Generate the relevant meta-data so that the IDE can pick that up • Description (field javadoc), default value, type ! Make that an isolated (configuration-only) thing 9 @snicoll
  10. Demo Unless otherwise indicated, these slides are 
 © 2013-2014

    Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ More conditions/features
  11. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Configuration to the rescue. Again. ! An auto-configuration can work in several ways (modes) • Ideally extra conditions should discriminate them • And a configuration key should offer a way to ‘force’ the mode explicitly ! Not always a sunny day • Achieving a good result for the end user may mean low-level checks • Usually more practical to custom conditions 11 @snicoll
  12. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Polishing 12 @snicoll
  13. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Polishing 13
  14. Demo Unless otherwise indicated, these slides are 
 © 2013-2014

    Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Polishing
  15. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Advanced customizations ! Try to put you in the shoes of the user ! @ConditionalOnMissingBean to replace auto-configuration partially • JMSConfiguration • EmbeddedJMS ! Callback interface to customize things • HornetQConfigurationCustomizer 15 @snicoll
  16. Demo Unless otherwise indicated, these slides are 
 © 2013-2014

    Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Advanced customisations
  17. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Your own Condition ! Many use cases already covered by Spring Boot ! Reuse SpringBootCondition base class • Provide sensible logging • Consistent error management ! Implement ConfigurationCondition to tune the registration phase • PARSE_CONFIGURATION: the whole @Configuration class isn’t processed if the condition does not match • REGISTER_BEAN: evaluated when the bean factory has more information ! Specify order wisely so that cheap conditions are evaluated first 17 @snicoll
  18. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ CLI customizations 18 package org.test @Grab('hornetq-jms-client') @EnableJms @Log class ThisWillActuallyRun { @JmsListener(destination = 'testQueue') def processMsg(String msg) { log.info("Received $msg") } }
  19. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ CLI customizations ! Parse your Groovy script • Grab extra dependencies • Auto-magically add imports ! Extend from CompilerAutoConfiguration • matches(ClassNode node) determines if the customisation should apply • applyDependencies(DependencyCustomizer customizer) add extra dependencies • applyImports(ImportCustomizer customizer) add extra imports 19 @snicoll
  20. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Wrapping Up ! Implementing your own auto-configuration is easy • Dealing with all the particularities of what you want to configure is the hard part ! All the infrastructure that Boot uses internally is available • We have a good range of “core” conditions already ! Creating custom conditions is cheap • Prefer that to über complex SpEL expressions ! Use configuration keys to expose configuration options • And make sure to generate the relevant meta-data for IDEs support ! (More advanced) use callback interface for fine-grained customizations 20 @snicoll
  21. Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 21 Learn More. Stay Connected. ! Start your project at start.spring.io ! Getting started guides ! Follow @springboot ! StackOverflow - #spring-boot Twitter: twitter.com/springcentral YouTube: spring.io/video LinkedIn: spring.io/linkedin Google Plus: spring.io/gplus @snicoll