JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

66
Spring Framework 4.0 The Next Generation Sam Brannen @sam_brannen

Transcript of JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

Page 1: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

Spring Framework 4.0 The Next Generation

Sam Brannen @sam_brannen

Page 2: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  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

|

Sam Brannen

Page 3: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Spring Framework

•  Java EE

•  OSGi

•  Agile Methodologies

•  Software Engineering Best Practices

|

Swiftmind: Areas of Expertise

Page 4: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Zurich, Switzerland

•  Twitter: @swiftmind

•  http://www.swiftmind.com

|

Swiftmind: How to find us

Page 5: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

A Show of Hands…

Page 6: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Spring 3.x in Review

•  Themes in 4.0

•  Java EE

•  Java SE

•  Spring 4 on Java 8

|

Agenda

Page 7: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

annotations, profiles

•  Spring Expression Language •  and its use in @Value injection

|

Spring 3.x: Component Model

Page 8: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Comprehensive REST support •  and other Spring @MVC additions

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

callbacks

|

Spring 3.x: Component Model

Page 9: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

•  Declarative scheduling •  trigger abstraction and cron support

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

|

Spring 3.x: Component Model

Page 10: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Embedded databases via <jdbc /> namespace

•  @Configuration classes & @ActiveProfiles

•  @WebAppConfiguration

•  @ContextHierarchy

•  Spring MVC Test framework

|

Spring 3.x: Testing

Page 11: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

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

|

Spring 3.x: Key Specifications

Page 12: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  JPA 2.0 •  persistence provider integration •  Spring transactions

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

|

Spring 3.x: Key Specifications

Page 13: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

Typical Annotated Component

Page 14: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

|

Composable Stereotypes

Page 15: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

Configuration Classes

Page 16: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

Spring 4.0 Themes

Page 17: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Java SE 6+

•  Java EE 6+ (Servlet 3.0 focused, Servlet 2.5

compatible)

•  All deprecated packages removed

•  Many deprecated methods removed as well

|

New Baseline

Page 18: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Minimum versions ~ mid 2010 now

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

|

Third-party Libraries

Page 19: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Lambda expressions

•  Method references

•  JSR-310 Date and Time

•  Repeatable annotations

•  Parameter name discovery

|

Java 8 Language & API Features

Page 20: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  A smooth out-of-the-box experience for

Groovy-based Spring applications

|

Groovy + Spring 4.0

Page 21: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

written in the Groovy language instead of Java

|

Groovy + Spring 4.0

Page 22: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

model

|

Groovy + Spring 4.0

Page 23: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  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 Bean Definitions

Page 24: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  @Conditional with programmatic Condition

implementations •  can react to rich context (existing bean definitions,

etc.) •  profile support now simply a ProfileCondition

implementation class

|

Conditional Bean Definitions

Page 25: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Custom annotations may override specific

attributes of meta-annotations

•  Purely convention-based

•  use of same attribute name

|

Annotation-based Components

Page 26: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

|

Annotation-based Components

Page 27: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  @Lazy on injection point

•  Alternative to Provider<MyTargetType>

|

Lazy Resolution Proxies

Page 28: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Ordered/@Order on candidate beans

•  Relative order within specific injection result

|

Ordered Injection of Lists & Arrays

Page 29: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

•  Generic factory methods supported in XML

config files •  Mockito, EasyMock, etc.

|

DI and Generics

Page 30: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  New org.springframework.messaging module

•  Extracted from Spring Integration

•  Core message and channel abstractions

|

spring-messaging

Page 31: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  WebSocket endpoint model along the lines of

Spring MVC

•  JSR-356 but also covering SockJS and STOMP

•  Endpoints using generic messaging patterns

|

WebSockets

Page 32: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Analogous to existing RestTemplate

•  Based on ListenableFuture return values

|

AsyncRestTemplate

Page 33: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

Spring and Java EE

Page 34: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  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

|

Java EE Support (1/2)

Page 35: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  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

|

Java EE Support (2/2)

Page 36: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  JMS 2.0 •  delivery delay, JMS 2.0 createSession() variants, etc.

•  JTA 1.2 •  javax.transaction.Transactional annotation

|

Enterprise APIs (1/2)

Page 37: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  JPA 2.1 •  unsynchronized persistence contexts

•  Bean Validation 1.1 •  method parameter and return value constraints

|

Enterprise APIs (2/2)

Page 38: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

Spring and Java SE

Page 39: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  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

|

Java SE Support (1/2)

Page 40: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  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

|

Java SE Support (2/2)

Page 41: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

Java 8 Programming Model

Page 42: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Delayed again...

•  Scheduled for GA in September 2013

•  Now just Developer Preview in September

•  OpenJDK 8 GA as late as March 2014 (!)

|

The State of Java 8

Page 43: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  IntelliJ: available since IDEA 12, released in

Dec 2012

•  Eclipse: announced for June 2014 (!)

•  Spring Tool Suite: Eclipse-based beta support

earlier

|

IDE Support for Java 8

Page 44: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  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 patched ASM

4.1 variant

|

Java 1.8 Bytecode Level

Page 45: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Different hash algorithms in use

•  Leading to different hash iteration order

•  Code shouldn't rely on such an order but

sometimes does

|

HashMap/HashSet Differences

Page 46: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Simple rule: interface with single method •  typically callback interfaces •  for example: Runnable, Callable •  formerly “single abstract method” (SAM) types •  now “functional interfaces”

|

Java 8 Lambda Conventions

Page 47: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Many Spring APIs are candidates for lambdas •  by naturally following the lambda interface

conventions

|

Lambda + Spring = Natural Fit

Page 48: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  JmsTemplate + MessageCreator: •  Message createMessage(Session session) throws

JMSException

|

Lambdas with JMS APIs

Page 49: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  TransactionTemplate + TransactionCallback: •  Object doInTransaction(TransactionStatus status)

|

Lambdas with Tx APIs

Page 50: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  JdbcTemplate + RowMapper: •  Object mapRow(ResultSet rs, int rowNum) throws

SQLException

|

Lambdas with JDBC APIs

Page 51: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

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 52: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

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 53: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

Method References (1/2) 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, "test"); }, this::mapPerson);}

Page 54: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

Method References (2/2) private Person mapPerson(ResultSet rs, int rowNum) throws SQLException { return new Person(rs.getString(1), rs.getInt(2));}

Page 55: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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;

|

JSR-310 Date and Time

Page 56: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

|

Repeatable Annotations

Page 57: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

|

Parameter Name Discovery (1/2)

Page 58: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

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

|

Parameter Name Discovery (2/2)

Page 59: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

|

In Closing…

Page 60: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Spring Framework 4.0 RC1: end of October

•  Spring Framework 4.0 RC2: mid November

•  Spring Framework 4.0 GA: end of 2013

|

Roadmap

Page 61: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  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

|

Upgrade Considerations

Page 62: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

Special thanks to Spring Framework project lead

Juergen Hoeller for permitting reuse of his content.

|

Acknowledgements

Page 63: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Spring Framework

•  http://projects.spring.io/spring-framework

•  Spring Forums

•  http://forum.spring.io

|

Spring Resources (1/2)

Page 64: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Spring JIRA

•  https://jira.springsource.org

•  Spring on GitHub

•  https://github.com/spring-projects/spring-framework

|

Spring Resources (2/2)

Page 65: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

•  Swiftmind Blog

•  http://www.swiftmind.com/blog

•  Spring Blog

•  http://spring.io/blog

|

Blogs

Page 66: JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation

Thanks for your attention!

@sam_brannen [email protected]

slideshare.net/sbrannen www. .com