Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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]

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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) { ... } }

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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") } }

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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