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

Spring Framework 4 on Java 8

Spring Framework 4 on Java 8

Spring has a track record of providing dedicated support for new Java generations in a timely fashion, and now it’s right about time to go Java 8: With Spring Framework 4.0, we're providing in-depth support for all relevant OpenJDK 8 features, including lambda expressions, JSR-310 Date and Time, parameter name discovery, and java.util.concurrent enhancements. This talk will illustrate basic Spring Framework 4.0 concepts, and selected Java 8 features within Spring's programming model, exploring the impact on application architectures.

Juergen Hoeller

March 25, 2014
Tweet

More Decks by Juergen Hoeller

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/ 1 Spring Framework 4 on Java 8 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/ Juergen Hoeller Spring Framework Lead 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/ 2 The State of the Art: Component Classes @Service @Lazy public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(AccountRepository repo) { ... } @Transactional public BookUpdate updateBook(Addendum addendum) { ... } }
  3. 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/ 3 The State of the Art: Configuration Classes @Configuration @Profile("standalone") @EnableTransactionManagement public class MyBookAdminConfig { @Bean @Scope("session") public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; } ... }
  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/ 4 Introducing Spring Framework 4.0 SPRING IO CORE: 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/
  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/ 5 Introducing Spring Framework 4.0 ▪ Ready for new application architectures • embedded web servers and non-traditional datastores • lightweight messaging and WebSocket-style architectures • custom asynchronous processing with convenient APIs ▪ A new baseline • Java SE 6+ (minimum API level: JDK 6 update 18, ~ early 2010) • Java EE 6+ (Servlet 3.0 focused, Servlet 2.5 compatible at runtime) • all deprecated packages removed • many deprecated methods removed as well
  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/ 6 Generics-based Injection Matching @Service public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(MyRepository<Account> repo) { ... } } @Bean public MyRepository<Account> myAccountRepository() { return new MyAccountRepositoryImpl(); }
  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/ 7 Composable Annotations with Overridable Attributes @Scope("session") @Retention(RetentionPolicy.RUNTIME) public @interface MySessionScoped { ScopedProxyMode proxyMode() default ScopedProxyMode.NO; } @Transactional(rollbackFor=Exception.class) @Retention(RetentionPolicy.RUNTIME) public @interface MyTransactional { boolean readOnly(); }
  8. 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/ 8 Many Further Container Refinements ▪ A generalized model for conditional bean definitions • based on @Conditional; see Spring Boot (projects.spring.io/spring-boot) ▪ @Autowired @Lazy on injection points • requesting a lazy-initialization proxy individually per injection point ▪ Ordered injection of arrays and lists • sorting injected beans by Ordered / @Order ▪ Target-class proxies for classes with arbitrary constructors • creating CGLIB proxies using Objenesis, not invoking any constructor
  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/ 9 Messaging, WebSocket, Async Processing ▪ General org.springframework.messaging module • core message and channel abstractions • endpoints using generic messaging patterns • @MessageMapping and co for annotated endpoints ▪ WebSocket endpoint model along the lines of Spring MVC • JSR-356 and native server support, with SockJS fallback option • STOMP for higher-level messaging on top of raw WebSocket ▪ AsyncRestTemplate • based on ListenableFuture return values
  10. 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/ 10 Spring Framework 4 and Java EE 7 ▪ JMS 2.0 • delivery delay, JMS 2.0 createSession variants etc ▪ JTA 1.2 • javax.transaction.Transactional annotation ▪ JPA 2.1 • unsynchronized persistence contexts ▪ Bean Validation 1.1 • method parameter and return value constraints ▪ JSR-236 Concurrency • Managed(Scheduled)ExecutorService including trigger support
  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/ 11 Spring Framework 4 and Java 8 SPRING IO CORE: 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/
  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/ 12 Spring Framework 4 and Java 8 ▪ First-class support for Java 8 language and API features • lambda expressions • method references • JSR-310 Date and Time • repeatable annotations • parameter name discovery ▪ Full runtime compatibility with JDK 8 • for Spring apps built against JDK 6/7 but running against JDK 8 • when moving existing apps to a JDK 8 based deployment platform
  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/ 13 Java 8 Lambda Conventions ▪ Many common Spring APIs are candidates for lambdas • through naturally following the lambda interface conventions • formerly "single abstract method" types, now "functional interfaces" ▪ Simple rule: interface with single method to be implemented • typically callback interfaces • classic interfaces falling into that category: Runnable, Callable ▪ Common functional interfaces introduced in Java 8 • Function, Predicate, Supplier in java.util.function • e.g. for use with Java 8's Collection Stream API
  14. 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/ 14 Lambda Conventions in Spring APIs ▪ JdbcTemplate • PreparedStatementSetter: void setValues(PreparedStatement ps) throws SQLException • RowMapper: Object mapRow(ResultSet rs, int rowNum) throws SQLException ▪ JmsTemplate • MessageCreator: Message createMessage(Session session) throws JMSException ▪ TransactionTemplate • TransactionCallback: Object doInTransaction(TransactionStatus status)
  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/ 15 Lambdas with Spring's JdbcTemplate (v1) JdbcTemplate jt = new JdbcTemplate(dataSource); jt.query("SELECT name, age FROM person WHERE dep = ?", ps -> ps.setString(1, "Sales"), (rs, rowNum) -> new Person(rs.getString(1), rs.getInt(2)));
  16. 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/ 16 Lambdas with Spring's JdbcTemplate (v2) JdbcTemplate jt = new JdbcTemplate(dataSource); jt.query("SELECT name, age FROM person WHERE dep = ?", ps -> { ps.setString(1, "Sales"); }, (rs, rowNum) -> { return new Person(rs.getString(1), rs.getInt(2)); });
  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/ 17 Method References with Spring's JdbcTemplate public List<Person> getPersonList(String department) { JdbcTemplate jt = new JdbcTemplate(this.dataSource); return jt.query("SELECT name, age FROM person WHERE dep = ?", ps -> ps.setString(1, "Sales"), this::mapPerson); } private Person mapPerson(ResultSet rs, int rowNum) throws SQLException { return new Person(rs.getString(1), rs.getInt(2)); }
  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/ 18 JSR-310 Date and Time import java.time.*; import org.springframework.format.annotation.*; public class Customer { // @DateTimeFormat(iso=ISO.DATE) private LocalDate birthDate; @DateTimeFormat(pattern="M/d/yy h:mm") private LocalDateTime lastContact; ... }
  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/ 19 Repeatable Annotations @Scheduled(cron = "0 0 12 * * ?") @Scheduled(cron = "0 0 18 * * ?") public void performTempFileCleanup() { ... } @Schedules({ @Scheduled(cron = "0 0 12 * * ?"), @Scheduled(cron = "0 0 18 * * ?") }) public void performTempFileCleanup() { ... }
  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/ 20 Parameter Name Discovery ▪ Spring's DefaultParameterNameDiscoverer • as of Spring Framework 4.0: aware of Java 8's parameter reflection • now checking Java 8 first (-parameters) • ASM-based reading of debug symbols next (-debug) @Controller public class MyMvcController { @RequestMapping(value="/books/{id}", method=GET) public Book findBook(@PathVariable long id) { return this.bookAdminService.findBook(id); } }
  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 To Upgrade or Not To Upgrade? ▪ Spring 3.2 does not support the 1.8 bytecode level • upgrade to Spring 4.0+ to enable Java 8 language features ▪ We strongly recommend an early upgrade to Spring 4 • Spring Framework 4.0 is still compatible with JDK 6 and 7 • Spring Framework 3.2 in maintenance mode already ▪ Spring Framework 4.0 GA released in December 2013 • current minor release: Spring Framework 4.0.3 (March 26th, 2014)
  22. 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/ 22 Outlook: Spring Framework 4.1 SPRING IO CORE: 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/
  23. 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/ 23 Key Themes for Spring Framework 4.1 ▪ Comprehensive web resource handling • resource pipelining, cache control refinements ▪ Caching support revisited • alignment with JCache 1.0 final, user-requested enhancements ▪ JMS support overhaul • alignment with messaging module, annotation-driven endpoints ▪ Performance improvements • application startup, SpEL expression evaluation ▪ Spring Framework 4.1 GA scheduled for early September 2014
  24. 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/ 24 Learn More. Stay Connected. ▪ Core framework: projects.spring.io/spring-framework ▪ Check out Spring Boot: projects.spring.io/spring-boot ▪ Upcoming releases: Spring Framework 4.0.3 on Mar 26 Spring Framework 4.0.4 on Apr 30 Twitter: twitter.com/springcentral YouTube: spring.io/video LinkedIn: spring.io/linkedin Google Plus: spring.io/gplus