Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introducing Spring Framework 4.0

Introducing Spring Framework 4.0

Slides from the Spring Framework 4.0 webinar as delivered on January 23rd, 2014. This deck covers the state of the Spring Framework 4.0.1 release, scheduled for release a few days later.

Juergen Hoeller

January 23, 2014
Tweet

More Decks by Juergen Hoeller

Other Decks in Programming

Transcript

  1. 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
  2. 3 The State of the Art @Service public class MyBookAdminService

    implements BookAdminService { @Autowired public MyBookAdminService(AccountRepository repo) { ... } @Transactional public BookUpdate updateBook(Addendum addendum) { ... } }
  3. 5 Configuration Classes @Configuration @Profile("standalone") @EnableTransactionManagement public class MyBookAdminConfig {

    @Bean public BookAdminService myBookAdminService() { MyBookAdminService service = new MyBookAdminService(); service.setDataSource(bookAdminDataSource()); return service; } ... }
  4. 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(); }
  5. 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); } }
  6. 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
  7. 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
  8. 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
  9. 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)); });
  10. 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; ... }
  11. 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() { ... }
  12. 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' } }
  13. 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
  14. 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(); }
  15. 18 Generics-based Injection Matching @Service public class MyBookAdminService implements BookAdminService

    { @Autowired public MyBookAdminService(MyRepository<Account> repo) { ... } } @Bean public MyRepository<Account> myAccountRepository() { return new MyAccountRepositoryImpl(); }
  16. 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
  17. 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
  18. 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); }
  19. 22 Higher-Level Messaging: STOMP on WebSocket @Controller public class MyStompController

    { @SubscribeEvent("/positions") public List<PortfolioPosition> getPortfolios(Principal user) { ... } @MessageMapping("/trade") public void executeTrade(Trade trade, Principal user) { ... } }
  20. 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
  21. 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+
  22. 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
  23. 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
  24. 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