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/ 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

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

Slide 3

Slide 3 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/ 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; } ... }

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/ 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/

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/ 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

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/ 6 Generics-based Injection Matching @Service public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(MyRepository repo) { ... } } @Bean public MyRepository myAccountRepository() { return new MyAccountRepositoryImpl(); }

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/ 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(); }

Slide 8

Slide 8 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/ 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

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/ 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

Slide 10

Slide 10 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/ 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

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/ 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/

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/ 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

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/ 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

Slide 14

Slide 14 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/ 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)

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/ 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)));

Slide 16

Slide 16 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/ 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)); });

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/ 17 Method References with Spring's JdbcTemplate public List 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)); }

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/ 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; ... }

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

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/ 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); } }

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

Slide 22

Slide 22 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/ 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/

Slide 23

Slide 23 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/ 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

Slide 24

Slide 24 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/ 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