Download - Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

Transcript
Page 1: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

Spring Framework 4.0 The Next Generation Sam Brannen @sam_brannen

Soft-Shake | Geneva, Switzerland | 24 October 2013

Page 2: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

2

Sam Brannen

•  Spring and Java Consultant @ Swiftmind

•  Java Developer for over 15 years

•  Spring Framework Core Committer since 2007

•  Spring Trainer •  Presenter on Spring, Java, OSGi, and testing

Page 3: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

3

Swiftmind

Your experts for Enterprise Java Areas of expertise •  Spring * •  Java EE •  OSGi •  Agile Methodologies •  Software Engineering Best Practices

Where you find us •  Zurich, Switzerland •  Twitter: @swiftmind •  http://www.swiftmind.com

Page 4: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

4

A Show of Hands…

Page 5: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

5

Agenda

•  Spring 3.x in Review

•  Themes in 4.0

•  Java EE

•  Java SE

•  Spring 4 on Java 8

Page 6: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

6

Spring 3.x in Review

Page 7: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

7

Spring 3.x: Component Model

•  Powerful annotated component model –  stereotypes, configuration classes, composable

annotations, profiles

•  Spring Expression Language (SpEL) –  XML config files –  @Value, @Cacheable, etc. –  JSPs

Page 8: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

8

Spring 3.x: Component Model

•  Comprehensive REST support –  and other Spring @MVC additions

•  Support for async MVC processing –  Spring MVC interacting with Servlet 3.0 async

callbacks

Page 9: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

9

Spring 3.x: Component Model

•  Declarative validation and formatting –  integration with JSR-303 Bean Validation

•  Declarative scheduling –  trigger abstraction and cron support

•  Declarative caching –  in-memory, Ehcache, etc.

Page 10: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

10

Spring 3.x: Testing

•  Embedded databases via <jdbc /> namespace

•  @Configuration classes & @ActiveProfiles

•  @WebAppConfiguration

•  @ContextHierarchy

•  Spring MVC Test framework

Page 11: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

11

Spring 3.x: Key Specs

•  JSR-330 –  Dependency Injection for Java –  @Inject, @Qualifier, Provider mechanism

•  JSR-303 –  Bean Validation 1.0 –  declarative constraints –  embedded validation engine

Page 12: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

12

Spring 3.x: Key Specs

•  JPA 2.0 –  persistence provider integration –  Spring transactions

•  Servlet 3.0 –  web.xml-free deployment –  async request processing

Page 13: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

13

Typical Annotated Component

Page 14: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

14

Composable Stereotypes

•  Combining meta-annotations on a custom stereotype •  Automatically detected: no configuration necessary!

Page 15: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

15

Configuration Classes

Page 16: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

16

Spring 4.0 Themes

Page 17: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

17

New Baselines

•  Java SE 6+

•  Java EE 6+ –  Servlet 3.0 focused, Servlet 2.5 compatible

•  All deprecated packages removed

•  Many deprecated methods removed as well

Page 18: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

18

Third Party Libraries

•  Minimum versions ~ mid 2010 now

•  For example –  Hibernate 3.6+ –  Quartz 1.8+ –  Ehcache 2.1+

Page 19: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

19

Java 8 Language and API Features

•  Lambda expressions

•  Method references

•  JSR-310 Date and Time

•  Repeatable annotations

•  Parameter name discovery

Page 20: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

20

Groovy + Spring 4.0

•  A smooth out-of-the-box experience for Groovy-based Spring applications

•  AOP adaptations –  special handling of GroovyObject calls –  consider a Spring application with all components

written in the Groovy language instead of Java

•  Groovy-based bean definitions –  formerly known as the Bean Builder in Grails –  now to live alongside Spring's configuration class

model

Page 21: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

21

Conditional Bean Definitions

•  A generalized model for conditional bean definitions –  a more flexible and more dynamic variant of bean

definition profiles (as known from Spring 3.1) –  can be used for smart defaulting –  see Spring Boot J

•  @Conditional with programmatic Condition implementations –  can react to rich context (existing bean definitions,

etc.) –  profile support now simply a ProfileCondition

implementation class

Page 22: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

22

Annotation-based Components

•  Custom annotations may override specific attributes of meta-annotations

•  Purely convention-based –  use of same attribute name

@MyTransactional(readOnly = true)@MySessionScope(scopedProxyMode = TARGET_CLASS)

Page 23: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

23

Lazy Resolution Proxies

•  @Lazy on injection point

•  Alternative to Provider<MyTargetType>

Page 24: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

24

Ordered Injection of Lists & Arrays

•  Ordered / @Order on candidate beans

•  Relative order within specific injection result

Page 25: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

25

DI and Generics

•  Type matching based on full generic type –  e.g., MyRepository<Customer>

•  Generic factory methods now fully supported in XML config files –  Mockito, EasyMock, etc.

Page 26: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

26

spring-messaging

•  New org.springframework.messaging module

•  Extracted from Spring Integration

•  Core message and channel abstractions

Page 27: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

27

WebSockets

•  WebSocket endpoint model along the lines of Spring MVC

•  JSR-356 but also covering SockJS and STOMP

•  Endpoints using generic messaging patterns

Page 28: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

28

AsyncRestTemplate

•  Analogous to existing RestTemplate

•  Based on ListenableFuture return values

Page 29: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

29

Spring and Java EE

Page 30: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

30

Java EE Support (1/2)

•  Spring 2.5 –  completed Java EE 5 support –  J2EE 1.3 à Java EE 5

•  Spring 3.0 –  introduced Java EE 6 support –  J2EE 1.4 à Java EE 6

Page 31: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

31

Java EE Support (2/2)

•  Spring 3.1 / 3.2 –  strong Servlet 3.0 focus –  J2EE 1.4 (deprecated) à Java EE 6

•  Spring 4.0 –  introduces explicit Java EE 7 support –  Java EE 5 (with JPA 2.0 feature pack) à Java EE 7

Page 32: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

32

Enterprise API Updates

•  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

Page 33: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

33

Spring and Java SE

Page 34: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

34

Java SE Support (1/2)

•  Spring 2.5 –  introduced Java 6 support –  JDK 1.4 à JDK 6

•  Spring 3.0 –  raised the bar to Java 5+ –  JDK 5 à JDK 6

Page 35: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

35

Java SE Support (2/2)

•  Spring 3.1 / 3.2 –  explicit Java 7 support –  JDK 5 à JDK 7

•  Spring 4.0 –  introduces explicit Java 8 support –  JDK 6 à JDK 8

Page 36: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

36

Java 8 Programming Model

Page 37: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

37

The State of Java 8

•  Delayed again...

•  Scheduled for GA in September 2013

•  Now just Developer Preview in September

•  OpenJDK 8 GA as late as March 2014 L

Page 38: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

38

IDE Support for Java 8

•  IntelliJ –  available since IDEA 12, released in Dec 2012

•  Eclipse –  announced for June 2014 L

•  Spring Tool Suite –  Eclipse-based beta support earlier

Page 39: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

39

Java 8 Bytecode Level

•  Generated by -target 1.8–  compiler's default

•  Not accepted by ASM 4.x –  Spring's bytecode parsing library

•  Spring Framework 4.0 comes with a patched (jarjar’ed) ASM 4.1 variant

Page 40: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

40

HashMap / HashSet Differences

•  Different hash algorithms in use

•  Leading to different hash iteration order

•  Code shouldn't rely on such an order but sometimes does

Page 41: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

41

Java 8 Lambda Conventions

Simple rule: interface with single method

–  typically callback interfaces

–  for example: Runnable, Callable

–  formerly “Single Abstract Method” (SAM) types

–  now “functional interfaces”

Page 42: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

42

Lambda + Spring = Natural Fit

Many Spring APIs are candidates for lambdas

–  by naturally following the lambda interface conventions

Page 43: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

43

Lambdas with JmsTemplate

MessageCreator

Message createMessage(Session session)throws JMSException

Page 44: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

44

Lambdas with TransactionTemplate

TransactionCallback

Object doInTransaction(TransactionStatus status)

Page 45: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

45

Lambdas with JdbcTemplate

RowMapper

Object mapRow(ResultSet rs, int rowNum)throws SQLException

Page 46: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

46

Ex: Lambdas with JdbcTemplate #1

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

Page 47: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

47

Ex: Lambdas with JdbcTemplate #2

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

Page 48: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

48

Method References

public List<Person> getPersonList(String department) { JdbcTemplate jt = new JdbcTemplate(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));}

Page 49: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

49

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

Page 50: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

50

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

JDK 8

JDK 6+

Container

Repeated

Page 51: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

51

Parameter Name Discovery

•  Java 8 defines a Parameter reflection type for methods –  application sources to be compiled with –parameters

•  Spring's StandardReflectionParameterNameDiscoverer–  reading parameter names via Java 8's new Parameter

type

•  Spring's DefaultParameterNameDiscoverer–  now checking Java 8 first (-parameters) –  ASM-based reading of debug symbols next (-debug)

Page 52: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

52

In Closing…

Page 53: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

53

Spring Framework 4.0 Roadmap

•  RC1: end of October

•  RC2: mid November

•  GA: end of 2013

Page 54: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

54

Upgrade Considerations

•  Spring 3.2 does not support 1.8 bytecode level –  upgrade to Spring 4.0 to enable Java 8 language

features

•  Spring Framework 4.0 still compatible with JDK 6 and 7

•  Spring Framework 3.2 is in maintenance mode

Page 55: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

55

Acknowledgements

Special thanks to Spring Framework project lead Juergen Hoeller for permitting reuse of his content.

Page 56: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

56

Spring Resources

•  Spring Framework –  http://projects.spring.io/spring-framework

•  Spring Forums –  http://forum.spring.io

•  Spring JIRA –  https://jira.springsource.org

•  Spring on GitHub –  https://github.com/spring-projects/spring-framework

Page 57: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

57

Blogs

•  Swiftmind Blog –  http://www.swiftmind.com/blog

•  Spring Blog –  http://spring.io/blog

Page 58: Spring Framework 4.0 - The Next Generation - Soft-Shake 2013

58

Q & A

Sam Brannen twitter: @sam_brannen www.slideshare.net/sbrannen www.swiftmind.com