Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Spring Framework 4 on Java 8 
Juergen Hoeller 
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permis...
Spring Framework 4 – a new baseline 
• Java SE 6+ 
• minimum API level: JDK 6 update 18, ~ early 2010 
• Java EE 6+ 
• Ser...
Feature themes in Spring Framework 4.0 
• Injection matching by full generic type 
• Generalized model for conditional bea...
Feature themes in Spring Framework 4.1 
• Annotated JMS listener methods 
• Comprehensive support for JCache (JSR-107) ann...
Agenda: Java 8 features in Spring 4.0 / 4.1 
• Lambda expressions 
• Method references 
• JSR-310 Date and Time 
• Repeata...
Java 8 lambda conventions 
• Simple rule: interfaces with single method to be implemented 
• Typically callback interfaces...
Lambda conventions with Spring APIs 
• TransactionTemplate with TransactionCallback: 
Object doInTransaction(TransactionSt...
Lambdas with Spring's JdbcTemplate (v1) 
JdbcTemplate jt = new JdbcTemplate(dataSource); 
jt.query("SELECT name, age FROM ...
Lambdas with Spring's JdbcTemplate (v2) 
JdbcTemplate jt = new JdbcTemplate(dataSource); 
jt.query("SELECT name, age FROM ...
Method references with Spring's JdbcTemplate 
public List<Person> getPersonList(String department) { 
JdbcTemplate jt = ne...
JSR-310 Date and Time 
import java.time.*; 
import org.springframework.format.annotation.*; 
public class Customer { 
// @...
Repeatable annotations 
@Scheduled(cron = "0 0 12 * * ?") 
@Scheduled(cron = "0 0 18 * * ?") 
public void performTempFileC...
Spring's parameter name discovery 
• As of 4.0: aware of Java 8's parameter reflection 
• Finally, first-class support for...
Parameter name discovery in action 
@Controller 
public class MyMvcController { 
@RequestMapping(value="/books/{id}", meth...
MVC handler methods: traditional required flag 
@Controller 
public class MyMvcController { 
@RequestMapping(value="/books...
New in 4.1: Java 8's java.util.Optional 
@Controller 
public class MyMvcController { 
@RequestMapping(value="/books/{id}",...
Injection points: traditional required flag 
@Service 
public class MyService { 
@Autowired(required=false) 
NotificationS...
New in 4.1: Optional at injection points 
@Service 
public class MyService { 
@Autowired 
Optional<NotificationService> no...
Injection points: ordered lists (as of 4.0) 
@Service @Order(1) 
public class MyServiceImpl implements MyService { ... } 
...
New in 4.1: @Order on @Bean methods 
@Bean @Order(1) 
public MyService myServiceX() { return new MyServiceImpl() } 
@Bean ...
New in 4.1: @javax.annotation.Priority for ordering 
@Service @Priority(1) 
public class MyServiceImpl implements MyServic...
Injection: selecting a @Primary candidate 
@Service @Primary 
public class MyServiceImpl implements MyService { ... } 
@Se...
Injection: @Order for primary candidate selection? 
@Service @Order(1) 
public class MyServiceImpl implements MyService { ...
Injection: @Order + @Primary works for selection 
@Service @Order(1) @Primary 
public class MyServiceImpl implements MySer...
Injection: @Priority for primary candidate selection 
@Service @Priority(1) // highest priority -> 'primary' role 
public ...
Injection: Qualifiers for candidate selection 
@Service @Qualifier("default") 
public class MyServiceImpl implements MySer...
Injection: generic types for selection (as of 4.0) 
@Service 
public class MyServiceImpl implements MyService<Entity> { .....
Summary: Java 8 features in Spring 4.0 / 4.1 
• Lambda expressions 
• Method references 
• JSR-310 Date and Time 
• Repeat...
Upcoming SlideShare
Loading in …5
×

Spring on Java 8

4,663 views

Published on

Speaker: Juergen Hoeller
Core Spring Track

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.

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Nice !! Download 100 % Free Ebooks, PPts, Study Notes, Novels, etc @ https://www.ThesisScientist.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • http://dbmanagement.info/Tutorials/Java.htm
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Spring on Java 8

  1. 1. Spring Framework 4 on Java 8 Juergen Hoeller © 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
  2. 2. Spring Framework 4 – 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 • Comprehensive support for Java SE 8 • language features and APIs • Explicit support for Java EE 7 level specifications • JMS 2.0, JTA 1.2, JPA 2.1, Bean Validation 1.1, JSR-236 2
  3. 3. Feature themes in Spring Framework 4.0 • Injection matching by full generic type • Generalized model for conditional bean definitions • Composable annotations with overridable attributes • General org.springframework.messaging module • WebSocket endpoint model along the lines of Spring MVC 3
  4. 4. Feature themes in Spring Framework 4.1 • Annotated JMS listener methods • Comprehensive support for JCache (JSR-107) annotations • Flexible resolution and transformation of static web resources • MVC views: declarative resolution, Groovy markup templates • WebSocket refinements: WebSocket scope, SockJS client 4
  5. 5. Agenda: Java 8 features in Spring 4.0 / 4.1 • Lambda expressions • Method references • JSR-310 Date and Time • Repeatable annotations • Parameter name discovery • java.util.Optional • Related new injection features: • Ordering, @Priority, generic types 5
  6. 6. Java 8 lambda conventions • Simple rule: interfaces with single method to be implemented • Typically callback interfaces such as Runnable or Callable: Executor.execute(Runnable) • Common functional interfaces in java.util.function: Function, Predicate, Supplier • A great use case: Java 8's Collection Stream API 6
  7. 7. Lambda conventions with Spring APIs • TransactionTemplate with TransactionCallback: Object doInTransaction(TransactionStatus status) • JdbcTemplate with PreparedStatementSetter: void setValues(PreparedStatement ps) throws SQLException • JdbcTemplate with RowMapper: Object mapRow(ResultSet rs, int rowNum) throws SQLException 7
  8. 8. 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))); 8
  9. 9. 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)); }); 9
  10. 10. 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)); } 10
  11. 11. 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; } 11
  12. 12. 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() { ... } 12
  13. 13. Spring's parameter name discovery • As of 4.0: aware of Java 8's parameter reflection • Finally, first-class support for parameter names in the JDK! • accessible via common Java reflection methods • Now checking Java 8 first -parameters • ASM-based reading of debug symbols next -debug 13
  14. 14. Parameter name discovery in action @Controller public class MyMvcController { @RequestMapping(value="/books/{id}", method=GET) public Book findBook(@PathVariable long id, @RequestHeader String country) { ... } } 14
  15. 15. MVC handler methods: traditional required flag @Controller public class MyMvcController { @RequestMapping(value="/books/{id}", method=GET) public Book findBook(@PathVariable long id, @RequestHeader(required=false) String country) { if (country != null) { ... } } } 15
  16. 16. New in 4.1: Java 8's java.util.Optional @Controller public class MyMvcController { @RequestMapping(value="/books/{id}", method=GET) public Book findBook(@PathVariable long id, @RequestHeader Optional<String> country) { country.ifPresent(value -> ...); } } 16
  17. 17. Injection points: traditional required flag @Service public class MyService { @Autowired(required=false) NotificationService notificationService; public Book findBook(long id) { If (notificationService != null) { ... } } } 17
  18. 18. New in 4.1: Optional at injection points @Service public class MyService { @Autowired Optional<NotificationService> notificationService; public Book findBook(long id) { notificationService.ifPresent(service -> ...); } } 18
  19. 19. Injection points: ordered lists (as of 4.0) @Service @Order(1) public class MyServiceImpl implements MyService { ... } @Service @Order(2) public class MyOtherServiceImpl implements MyService { ... } @Autowired List<MyService> myServices; // MyServiceImpl first 19
  20. 20. New in 4.1: @Order on @Bean methods @Bean @Order(1) public MyService myServiceX() { return new MyServiceImpl() } @Bean @Order(2) public MyService myServiceY() { return new MyOtherServiceImpl() } @Autowired List<MyService> myServices; // MyServiceImpl first 20
  21. 21. New in 4.1: @javax.annotation.Priority for ordering @Service @Priority(1) public class MyServiceImpl implements MyService { ... } @Service @Priority(2) public class MyOtherServiceImpl implements MyService { ... } @Autowired List<MyService> myServices; // MyServiceImpl first 21
  22. 22. Injection: selecting a @Primary candidate @Service @Primary public class MyServiceImpl implements MyService { ... } @Service public class MyOtherServiceImpl implements MyService { ... } @Autowired MyService myService; // MyServiceImpl getting selected! 22
  23. 23. Injection: @Order for primary candidate selection? @Service @Order(1) public class MyServiceImpl implements MyService { ... } @Service @Order(2) public class MyOtherServiceImpl implements MyService { ... } @Autowired MyService myService; // FAILS: @Order lacks 'primary' semantics 23
  24. 24. Injection: @Order + @Primary works for selection @Service @Order(1) @Primary public class MyServiceImpl implements MyService { ... } @Service @Order(2) public class MyOtherServiceImpl implements MyService { ... } @Autowired MyService myService; // MyServiceImpl getting selected! 24
  25. 25. Injection: @Priority for primary candidate selection @Service @Priority(1) // highest priority -> 'primary' role public class MyServiceImpl implements MyService { ... } @Service @Priority(2) public class MyOtherServiceImpl implements MyService { ... } @Autowired MyService myService; // MyServiceImpl getting selected! 25
  26. 26. Injection: Qualifiers for candidate selection @Service @Qualifier("default") public class MyServiceImpl implements MyService { ... } @Service public class MyOtherServiceImpl implements MyService { ... } @Autowired @Qualifier("default") MyService myService; // MyServiceImpl getting selected! 26
  27. 27. Injection: generic types for selection (as of 4.0) @Service public class MyServiceImpl implements MyService<Entity> { ... } @Service public class MyOtherServiceImpl implements MyService<Event> { } @Autowired MyService<Entity> myService; // MyServiceImpl getting selected! 27
  28. 28. Summary: Java 8 features in Spring 4.0 / 4.1 • Lambda expressions • Method references • JSR-310 Date and Time • Repeatable annotations • Parameter name discovery • java.util.Optional • Related new injection features: • Ordering, @Priority, generic types 28

×