Zero Effort Spring

Download Zero Effort Spring

Post on 10-May-2015




1 download

Embed Size (px)


Speakers: Dave Syer and Phil Webb Are you an impatient programmer who wishes the keyboard could keep up with you? If you've ever wished Java and Spring could do more to keep up, then this is the place to be. We present Spring Boot, a toolkit and runtime platform that will get you up and running with Spring-powered, production-grade applications and services faster than you believed possible. The goals are: Radically faster and widely accessible getting started experience for Spring development Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration) First class support for REST-ful services, modern web applications, batch jobs, and enterprise integration Applications that adapt their behavior or configuration to their environment Optionally use Groovy features like DSLs and AST transformations to accelerate the implementation of basic business requirements We illustrate how these goals can be achieved through a series of demonstrations, and in-depth reviews of the design principles and codebase of new features in Spring Framework 4.0 and in the wider Spring Ecosystem .


<ul><li>1.1 of 37Spring Boot Dave Syer, Phil Webb, 2013 Twitter: @david_syer, @phillip_webb Email: [dsyer, pwebb] (Introduction to Spring Boot)03/09/13 14:50</li></ul> <p>2. 2 of 3703/09/13 14:50 3. 3 of 37Agenda Quick overview of goals and high level features Getting started demo Behind the scenes of @EnableAutoConfiguration Adding new features to Spring Boot03/09/13 14:50 4. 4 of 37Introduction Spring Boot: Focuses attention at a single point (as opposed to large collection of spring-* projects) A tool for getting started very quickly with Spring Common non-functional requirements for a "real" application Exposes a lot of useful features by default Gets out of the way quickly if you want to change defaults An opportunity for Spring to be opinionated03/09/13 14:50 5. 5 of 37Focus Attention03/09/13 14:50 6. 6 of 37Spring Boot Goals Provide a radically faster and widely accessible getting started experience Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration) Absolutely no code generation and no requirement for XML configuration03/09/13 14:50 7. 7 of 37Getting Started Really Quickly app.groovy: @Controller class Application { @RequestMapping('/') @ResponseBody String home() { 'Hello World!' } }then $ spring run app.groovy03/09/13 14:50 8. 8 of 37Getting StartedDEMO03/09/13 14:50 9. 9 of 37Getting Started In Java ... @Controller @EnableAutoConfiguration public class Application { @RequestMapping("/") @ResponseBody public String home() { "Hello World!"; } public static main(String[] args) {, args); } } 03/09/13 14:50 10. 10 of 37Getting Started In Java ...then $ mvn package $ java -jar target/*.jar(or use gradle equivalents)03/09/13 14:50 11. 11 of 37Getting StartedDEMO03/09/13 14:50 12. 12 of 37What Just Happened? SpringApplication: convenient way to write a main() method that loads a Spring context @EnableAutoConfiguration: optional annotation that adds stuff to your context, including... EmbeddedServletContainerFactory: added to your context if a server is available on the classpath CommandLineRunner: a hook to run application-specific code after the context is created JarLauncher was added to the JAR file03/09/13 14:50 13. 13 of 37Spring Boot Modules Spring Boot - main library supporting the other parts of Spring Boot Spring Boot Autoconfigure - single @EnableAutoConfiguration annotation creates a whole Spring context Spring Boot Starters - a set of convenient dependency descriptors that you can include in your application. Spring Boot CLI - compiles and runs Groovy source as a Spring application Spring Boot Actuator - comman non-functional features that make an app instantly deployable and supportable in production Spring Boot Tools - for building and executing self-contained JAR and WAR archives Spring Boot Samples - a wide range of sample apps03/09/13 14:50 14. 14 of 37Spring Boot Module Relations03/09/13 14:50 15. 15 of 37Binding to Command Line Arguments SpringApplication binds its own bean properties to command line arguments, and then adds them to the Spring Environment, e.g. $ java -jar target/*.jar --server.port=900003/09/13 14:50 16. 16 of 37Externalizing Configuration to Properties Just put in your classpath, e.g. server.port: 900003/09/13 14:50 17. 17 of 37Using YAML Just put application.yml in your classpath application.yml server: port: 9000Both properties and YAML add entries with period-separated paths to the Spring Environment.03/09/13 14:50 18. 18 of 37Binding External Configuration To Beans @ConfigurationProperties(prefix="mine") public class MinePoperties { private Resource location; private boolean skip = true; // ... getters and setters } mine.location: classpath:mine.xml mine.skip: false03/09/13 14:50 19. 19 of 37Customizing Configuration Location Set - default application, can be commaseparated list spring.config.location - a Resource path, overrides name e.g. $ java -jar target/*.jar 14:50 20. 20 of 37Spring Profiles Activate external configuration with a Spring profile file name convention e.g. or nested documents: application.yml defaults: etc... --spring: profiles: development,postgresql other: stuff: more stuff...Set the default spring profile in external configuration, e.g: default,postgresql 03/09/13 14:50 21. 21 of 37Adding some Autoconfigured Behaviour Extend the demo and see what we can get by just modifying the classpath, e.g. Add an in memory database Add a Tomcat connection pool03/09/13 14:50 22. 22 of 37Adding A UI with Thymeleaf Add Thymeleaf to the classpath and see it render a view Spring Boot Autoconfigure has added all the boilerplate stuff Common configuration options via spring.thymeleaf.*, e.g. spring.thymeleaf.prefix:classpath:/templates/ (location of templates) spring.tjymeleaf.cache:true (set to false to reload templates when changed) Extend and override: add Thymeleaf IDialect beans add thymeleafViewResolver add SpringTemplateEngine add defaultTemplateResolver03/09/13 14:50 23. 23 of 37Currently Available Autoconfigured Behaviour Embedded servlet container (Tomcat or Jetty) DataSource and JdbcTemplate JPA Spring Data JPA (scan for repositories) Thymeleaf Batch processing Reactor for events and async processing Actuator features (Security, Audit, Metrics, Trace)03/09/13 14:50 24. 24 of 37Building a WAR We like launchable JARs, but you can still use WAR format if you prefer. Spring Boot Tools take care of repackaging a WAR to make it executable. If you want a WAR to be deployable (in a "normal" container), then you need to use SprinBootServletInitializer instead of or as well as SpringApplication.03/09/13 14:50 25. 25 of 37The Actuator Adds common non-functional features to your application and exposes MVC endpoints to interact with them. Security Secure endpoints: /metrics, /health, /trace, /dump, /shutdown, /beans Audit /info If embedded in a web app or web service can use the same port or a different one (and a different network interface).03/09/13 14:50 26. 26 of 37Adding Security Use the Actuator Add Spring Security to classpath03/09/13 14:50 27. 27 of 37Logging Spring Boot provides default configuration files for 3 common logging frameworks: logback, log4j and java.util.logging Starters (and Samples) use logback External configuration and classpath influence runtime behaviour LoggingApplicationContextInitializer sets it all up03/09/13 14:50 28. 28 of 37Customizing the ApplicationContext Creation Add external configuration (System properties, OS env vars, config file, command line arguments) Add ApplicationContextInitializer implementations and enable in application.properties03/09/13 14:50 29. 29 of 37Customizing the @EnableAutoConfiguration Behaviour Add JAR with META-INF/spring.factories entry for EnableAutoConfiguration All entries from classpath merged and added to context03/09/13 14:50 30. 30 of 37Customizing the CLI Uses standard Java META-INF/services scanning CompilerAutoConfiguration: add dependencies and imports CommandFactory: add commands via a custom CommandFactory in META-INF/services E.g. can add script commands (written in Groovy) $ spring foo ...Looks for foo.groovy in ${SPRING_HOME}/bin and ${SPRING_HOME}/ext by default03/09/13 14:50 31. 31 of 37Customizing Servlet Container Properties Some common features exposed with external configuration, e.g. server.port (see ServerProperties bean) Add bean of type EmbeddedServletContainerCustomizer - all instances get a callback Add bean of type EmbeddedServletContainerFactory (replacing auto-configured one)03/09/13 14:50 32. 32 of 37Spring Boot Loader Motivation: existing solutions for executable JAR are not very robust; executable WAR is very tricky to create. Response: JarLauncher and WarLauncher with specialized ClassLoader implementations that can find resources in nested JARs (e.g. lib/*.jar or WEB-INF/lib/*.jar)03/09/13 14:50 33. 33 of 37Maven and Gradle Tooling Create an executable archive (JAR or WAR) Maven plugin (using spring-boot-starter-parent): ${project.groupId} spring-boot-maven-plugin $ mvn package Gradle plugin: apply plugin: 'spring-boot' $ gradle repackage03/09/13 14:50 34. 34 of 37Testing with Spring Test (MVC) SpringApplication is an opinionated creator of an ApplicationContext, but most of the behaviour is encapsulated in ApplicationContextInitializer implementations. To reproduce the behaviour of you app in an integration test it is useful to duplicate those features you can use the corresponding initializers. Example if you have externalized configuration:03/09/13 14:50 35. 35 of 37@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = IntegrationTestsConfiguration.class, initializers = ConfigFileApplicationContextInitializer.class) public class IntegrationTests { // Normal Spring Test stuff }03/09/13 14:50 36. 36 of 37Links Spring Boot on Github Documentation Twitter: @david_syer Email: dsyer@vmware.com03/09/13 14:50 37. 37 of 37Spring Boot Intro - END03/09/13 14:50 </p>


View more >