Slide 1

Slide 1 text

1 © 2013 Pivotal Introducing Spring Framework 4.0 (4.0.1) Juergen Hoeller Spring Framework Lead Pivotal

Slide 2

Slide 2 text

2 Why do Java Developers choose Spring today? GRAILS Full-stack, Web XD Stream, Taps, Jobs BOOT Bootable, Minimal, Ops-Ready Web, Integration, Batch WEB Controllers, REST, WebSocket INTEGRATION Channels, Adapters, Filters, Transformers BATCH Jobs, Steps, Readers, Writers BIG DATA Ingestion, Export, Orchestration, Hadoop DATA NON-RELATIONAL RELATIONAL CORE GROOVY FRAMEWORK SECURITY REACTOR

Slide 3

Slide 3 text

3 The State of the Art @Service public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(AccountRepository repo) { ... } @Transactional public BookUpdate updateBook(Addendum addendum) { ... } }

Slide 4

Slide 4 text

4 Composable Annotations @Service @Scope("session") @Primary @Transactional(rollbackFor=Exception.class) @Retention(RetentionPolicy.RUNTIME) public @interface MyService {} @MyService public class MyBookAdminService { ... }

Slide 5

Slide 5 text

5 Configuration Classes @Configuration @Profile("standalone") @EnableTransactionManagement public class MyBookAdminConfig { @Bean public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; } ... }

Slide 6

Slide 6 text

6 Matching an Injection Point to a Factory Method @Service public class MyBookAdminService implements BookAdminService { @Autowired @Qualifier("production") public MyBookAdminService(AccountRepository repo) { ... } } @Bean @Qualifier("production") public AccountRepository myAccountRepository() { return new MyAccountRepositoryImpl(); }

Slide 7

Slide 7 text

7 Annotated MVC Controllers @Controller public class MyMvcController { @RequestMapping(value="/books/{id}", method=GET) public Book findBook(@PathVariable long id) { return this.bookAdminService.findBook(id); } @RequestMapping("/books/new") public void newBook(Book book) { this.bookAdminService.storeBook(book); } }

Slide 8

Slide 8 text

8 Spring Framework 4.0 SPRING IO CORE:

Slide 9

Slide 9 text

9 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 10

Slide 10 text

10 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 11

Slide 11 text

11 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, etc

Slide 12

Slide 12 text

12 Lambdas with Spring's JdbcTemplate 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))); 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 13

Slide 13 text

13 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 14

Slide 14 text

14 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 15

Slide 15 text

15 Spring Framework 4 and Groovy import org.mypackage.domain.Person; beans { xmlns util: 'http://www.springframework.org/schema/util' person1(Person) { name = "homer" age = 45 props = [overweight:true, height:"1.8m"] children = ["bart", "lisa"] } util.list(id: 'foo') { value 'one' value 'two' } }

Slide 16

Slide 16 text

16 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 e.g. be used for smart defaulting ● see Spring Boot (projects.spring.io/spring-boot) ■ @Conditional with programmatic Condition implementations ● can react to rich context (existing bean definitions etc) ● profile support now simply a ProfileCondition impl class ● enabling new deployment styles

Slide 17

Slide 17 text

17 Composable Annotations with Overridable Attributes @Scope(value="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 18

Slide 18 text

18 Generics-based Injection Matching @Service public class MyBookAdminService implements BookAdminService { @Autowired public MyBookAdminService(MyRepository repo) { ... } } @Bean public MyRepository myAccountRepository() { return new MyAccountRepositoryImpl(); }

Slide 19

Slide 19 text

19 Many Further Container Refinements ■ @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 ■ Composable annotations in the test context framework ● e.g. @ContextConfiguration, @WebAppConfiguration, @ActiveProfiles ■ Time zone management with dedicated Spring MVC support ● through LocaleContextResolver and TimeZoneAwareLocaleContext

Slide 20

Slide 20 text

20 Messaging, WebSocket, Async Processing ■ General org.springframework.messaging module ● core message and channel abstractions ● endpoints using generic messaging patterns ■ 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 21

Slide 21 text

21 WebSocketHandler with SockJS Fallback @Configuration @EnableWebSocket public class MyWebSocketConfig implements WebSocketConfigurer { public void registerWebSocketHandlers( WebSocketHandlerRegistry registry){ WebSocketHandler echoHandler = new EchoHandler(); registry.addHandler(echoHandler, "/echo").withSockJS(); } } public interface WebSocketHandler { void handleMessage(WebSocketSession s, WebSocketMessage> m); }

Slide 22

Slide 22 text

22 Higher-Level Messaging: STOMP on WebSocket @Controller public class MyStompController { @SubscribeEvent("/positions") public List getPortfolios(Principal user) { ... } @MessageMapping("/trade") public void executeTrade(Trade trade, Principal user) { ... } }

Slide 23

Slide 23 text

23 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 24

Slide 24 text

24 Common Server Generations and Third-Party Libraries ■ Tomcat 6.0.33+ (WebSocket on Tomcat 7.0.47+ and 8.0) ■ Jetty 7.5+ (WebSocket on Jetty 9.0 and 9.1) ■ JBoss 6.1+ (WebSocket on WildFly 8.0) ■ GlassFish 3.1+ (WebSocket on GlassFish 4.0) ■ WebLogic 10.3.4+ (with JPA 2.0 patch applied) ■ WebSphere 7.0.0.9+ (with JPA 2.0 feature pack) ■ We generally support library versions <=3 years back ● Spring Framework 4.0: minimum versions ~ early 2011 ● e.g. Hibernate 3.6+, EhCache 2.1+, Quartz 1.8+, Jackson 1.8+

Slide 25

Slide 25 text

25 To Upgrade or Not To Upgrade? ■ Spring 3.2.x does not support the 1.8 bytecode level ● However, it does support running JDK 6/7 based apps on JDK 8 ■ We strongly recommend an early upgrade to Spring 4 ● Spring Framework 3.2 is in maintenance mode already ● See github.com/spring-projects/spring-framework/wiki/ Migrating-from-earlier-versions-of-the-Spring-Framework ■ Spring Framework 4.0 GA released in December 2013 ■ Spring Framework 4.0.1 to be released on Monday (January 27th) ■ Spring Framework 4.0.2 scheduled for late March 2014

Slide 26

Slide 26 text

26 Next Iteration: 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 RC1 in June 2014 ■ Spring Framework 4.1 GA in August 2014

Slide 27

Slide 27 text

27 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.1 on Jan 27 Spring Framework 4.0.2 on Mar 25 Twitter: twitter.com/springcentral YouTube: spring.io/video LinkedIn: spring.io/linkedin Google Plus: spring.io/gplus