Spring 4-groovy


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Spring 4-groovy

  1. 1. © 2012 SpringSource, A division of VMware. All rights reservedwww.springsource.orgSpring 4 Embracing Groovy – A Work in ProgressJürgen Höller, Principal Engineer, SpringSource
  2. 2. 22www.springsource.orgReview: Spring 3 Component Model Themes Powerful annotated component model• stereotypes, configuration classes, composable annotations Spring Expression Language• and its use in value injection Comprehensive REST support• and other Spring @MVC additions Support for async MVC processing• Spring MVC interacting with Servlet 3.0 async callbacks Declarative validation and formatting• integration with JSR-303 Bean Validation Declarative scheduling• trigger abstraction, cron support Declarative caching
  3. 3. 33www.springsource.orgA Typical Annotated Component@Servicepublic class MyBookAdminService implements BookAdminService {@Injectpublic MyBookAdminService(AccountRepository ar) {…}@Transactionalpublic BookUpdate updateBook(Addendum addendum) {…}}
  4. 4. 44www.springsource.orgConfiguration Classes@Configurationpublic class MyBookAdminConfig {@Beanpublic BookAdminService myBookAdminService() {MyBookAdminService service = new MyBookAdminService();service.setDataSource(bookAdminDataSource());return service;}@Beanpublic DataSource bookAdminDataSource() {…}}
  5. 5. 55www.springsource.orgNext Stop: Spring Framework 4.0 First-class support for Java 8 language and API features• lambda expressions• JSR-310 Date and Time• java.util.concurrency updates First-class support for Groovy (in particular: Groovy 2)• Groovy-based bean definitions (a.k.a. Grails Bean Builder)• AOP treatment for Groovy classes A WebSocket endpoint model along the lines of Spring MVC• deploying Spring-defined endpoint beans to a WebSocket runtime• using JSR-356 compliant runtimes or alternative engines
  6. 6. 66www.springsource.orgSpring 4.0: Upcoming Enterprise Specs 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 Utilities• EE-compliant TaskScheduler backend with trigger support JSR-107 JCache• standard CacheManager backend, standard caching annotations
  7. 7. 77www.springsource.orgSpring and Common Java SE Generations 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 Spring 3.1/3.2: explicit Java 7 support• JDK 5 – JDK 7 Spring 4.0 introducing explicit Java 8 support now• JDK 6 – JDK 8
  8. 8. 88www.springsource.orgSpring and Common Java EE Generations 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 Spring 3.1/3.2: strong Servlet 3.0 focus• J2EE 1.4 (deprecated) – Java EE 6 Spring 4.0 introducing explicit Java EE 7 support now• Java EE 5 (with JPA 2.0 feature pack) – Java EE 7
  9. 9. 99www.springsource.orgThe 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 (!) IDE support for Java 8 language features• IntelliJ: available since IDEA 12, released in December 2012• Eclipse: announced for June 2014 (!)• Spring Tool Suite: trying to get some Eclipse-based support earlier Spring Framework 4.0 scheduled for GA in October 2013• with best-effort Java 8 support on OpenJDK 8 Developer Preview
  10. 10. 1010www.springsource.orgJSR-310 Date-Time Specialized date and time value types in java.time package• replacing java.util.Date/Calendar, along the lines of the Joda-Time project• Spring 4.0: annotation-driven date formattingpublic class Customer {// @DateTimeFormat(iso=ISO.DATE)private LocalDate birthDate;@DateTimeFormat(pattern="M/d/yy h:mm")private LocalDateTime lastContact;}
  11. 11. 1111www.springsource.orgLambda Conventions Many common Spring APIs are candidates for lambdas• through naturally following the lambda interface conventions• formerly "single abstract method" types, now "functional interfaces" JdbcTemplate• ResultSetExtractor, RowCallbackHandler, RowMapper JmsTemplate• MessageCreator, MessagePostProcessor, BrowserCallback TaskExecutor• Runnable, Callable
  12. 12. 1212www.springsource.orgJdbcTemplate 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));});Java 8 Lambdas with Springs JdbcTemplate
  13. 13. 1313www.springsource.orgpublic 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;}private Person mapPerson(ResultSet rs, int rowNum)throws SQLException {return new Person(rs.getString(1), rs.getInt(2));}Java 8 Method References with Springs JdbcTemplate
  14. 14. 1414www.springsource.orgGroovy Closures versus Lambdas Java 8 lambdas are quite restricted in their applicability• always coerced into a specific target expression• primarily a replacement for anonymous inner classes At the same time, lambdas are syntactically attractive• concise (at least for Java terms)• identifying overloaded methods through parameter name clause Groovy closures are a quite different beast• a powerful first-class language and API construct• mandatory "as“ clause for type coercion
  15. 15. 1515www.springsource.orgSpring 4 and Groovy Closures Groovy closures to be equally attractive for Springs callback APIs• i.e. equally applicable to Java 8s functional interface conventions• providing use cases for Groovy language enhancements Groovy 2.2: no "as“ clause necessary for unique type scenarios• like with Java 8 lambdas, infer type from target context• "as“ clause just to be used for overloaded methods Groovy 3.0: researching lambda/closure interoperability• support for dedicated lambda syntax in Groovy?• applying lambda expressions to Groovy Closure arguments?
  16. 16. 1616www.springsource.orgGroovy-based Bean Definitions Essentially, Grails Bean Builder turning into a Spring feature• revised bean builder API but same configuration format• with Grails 3.0 building on itbeans {dataSource(BasicDataSource) {driverClassName = "org.h2.Driver"url = "jdbc:h2:mem:grailsDB"username = "sa"password = ""}}
  17. 17. 1717www.springsource.orgAOP Treatment for Groovy Classes Exclusion of internal Groovy interfaces from AOP proxying• special detection of GroovyObject methods• done in Grails already• now in Springs core AOP framework Applying aspects through Groovy AST transformations• e.g. @Transactional processing• Spring has separate "proxy" and "aspectj" modes already• researching what a corresponding "groovy" mode could look like• only makes sense for 100% Groovy-based application classes
  18. 18. 1818www.springsource.orgGroovy as Language of Choice for Spring Apps The idea is simple:• implementing a traditional Spring-style application architecture• but with 100% Groovy instead of Java Springs programming and configuration style with Groovy• with the entire set of traditional Spring guidelines applying• just replace an apps Java source files with Groovy source files Groovy 2s static compilation mode might be a good fit• using @CompileStatic or @TypeChecked for semantics closer to Java• potentially attractive to first-time Groovy adopters
  19. 19. 1919www.springsource.orgSpring Framework 4.0 M1 & M2 4.0 M1 (May 2013)• general pruning and dependency upgrades (JDK 6+, JPA 2.0+, etc)• initial Java 8 support based on OpenJDK 8 build 88• JMS 2.0, JTA 1.2, JPA 2.1, Bean Validation 1.1, JSR-236 Concurrency• initial WebSocket endpoint model 4.0 M2 (July 2013)• enhanced use of attributes on stereotype annotations• generic type support for injection points• Groovy-based bean definitions• AOP treatment for Groovy classes
  20. 20. 2020www.springsource.orgQ & A