Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012

19,163 views

Published on

Spring 3.1 introduced several eagerly awaited features including bean definition profiles (a.k.a., environment-specific configuration), enhanced Java-based application and infrastructure configuration (a la XML namespaces), and a new cache abstraction. This session will provide attendees a high-level overview of these major new features plus a quick look at additional enhancements to the framework such as the new c: namespace for constructor arguments, support for Servlet 3.0, improvements to Spring MVC and REST, and Spring's new integration testing support for profiles and configuration classes. In addition, this talk will introduce new features under development in the Spring 3.2 roadmap.

Published in: Technology, Education
0 Comments
26 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
19,163
On SlideShare
0
From Embeds
0
Number of Embeds
1,034
Actions
Shares
0
Downloads
0
Comments
0
Likes
26
Embeds 0
No embeds

No notes for slide

Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012

  1. 1. Spring 3.1 to 3.2 in a Nutshell Sam Brannen Swiftmind
  2. 2. Speaker Profile• Spring & Java consultant @ Swiftmind• Developing Java for over 14 years• Spring Framework Core Developer• Spring Trainer• Lead author of “Spring in a Nutshell”
  3. 3. Show of hands…
  4. 4. Agenda• Major Themes in 3.x• Environment and Profiles• Java-based Configuration• Testing• Caching• MVC and REST• Servlet 3.0• 3.2 Roadmap
  5. 5. Major Themes in Spring 3.0
  6. 6. Java-based configuration
  7. 7. custom stereotypes
  8. 8. annotated factory methods
  9. 9. JSR-330 – DI for Java
  10. 10. Spring Expression Language
  11. 11. REST support in Spring MVC
  12. 12. Portlet API 2.0
  13. 13. JSR-303 – bean validation
  14. 14. Java EE 6 support: JPA 2.0, JSF 2.0
  15. 15. Major Themes in Spring 3.1– Environment abstraction– Java-based application configuration– @Configuration class test support– High-level cache API– Customizable @MVC processing– Flash maps and redirect attributes– Explicit support for Servlet 3.0
  16. 16. Environment and Profiles
  17. 17. Environment Abstraction– Injectable environment abstraction API • org.springframework.core.env.Environment– Two core concepts • Property Sources • Bean ProfilesProperty Source: Bean Profile:A variety of sources: property A logical group of beanfiles, system properties, definitions. Registered only ifservlet context, JNDI, etc. the profile is active.
  18. 18. Property Source Abstraction– Property source– Property resolution– Placeholders
  19. 19. PropertySource(s)– PropertySource • a single property source– PropertySources • a hierarchy of PropertySource objects • potentially varying across deployment environments
  20. 20. Property Resolution SPI– org.springframework.core.env.PropertyResolver– Environment extends PropertyResolver
  21. 21. Custom Placeholder Resolution– dependent on the actual environment– PropertySourcesPlaceholderConfigurer supersedes PropertyPlaceholderConfigurer
  22. 22. Managing Property Sources– Stand-alone code– Web application • Implement ApplicationContextInitializer • Register via contextInitializerClasses context parameter in web.xml
  23. 23. Accessing Properties– By injecting the Environment
  24. 24. Bean Definition Profiles– Logical grouping of bean definitions • for activation in specific environments • e.g., dev, staging, prod • possibly different deployment platforms– Configuration • XML via <beans profile=“…”> • Java-based configuration via @Profile
  25. 25. Configuring Profiles in XML– All bean definitions– Subset of bean definitions
  26. 26. Configuring Profiles in Java Config– @Profile can also be used on components • @Component, @Service, @Repository, etc. 26
  27. 27. Profile Activation Options– programmatically– system property– in web.xml– in tests via @ActiveProfiles
  28. 28. Activating Profiles… programmatically
  29. 29. Activating Profiles… via system properties-Dspring.profiles.active=“dev”-Dspring.profiles.default=“common”
  30. 30. Activating Profiles… in web apps
  31. 31. Activating Profiles… in integration tests
  32. 32. Java-based Configuration
  33. 33. Java Config ~= XML– XML namespaces  @Enable*– FactoryBeans  builders– GenericXmlContextLoader  AnnotationConfigContextLoader– Not a one-to-one mapping • Make the most of what Java has to offer • Intuitive annotation-oriented container configuration
  34. 34. Typical Infrastructure Setup– Transactions– Scheduling– MVC customization– AOP
  35. 35. @Enable* Annotations– Applied at the class-level on @Configuration classes– Roughly equivalent to their XML namespace counterparts
  36. 36. @Enable* in 3.1– @EnableTransactionManagement– @EnableCaching– @EnableAsync– @EnableScheduling– @EnableAspectJAutoProxy– @EnableLoadTimeWeaving– @EnableSpringConfigured– @EnableWebMvc
  37. 37. Hibernate and JPA– Hibernate LocalSessionFactoryBuilder API • Hibernate 4 replacement for both LocalSessionFactoryBean and AnnotationSessionFactoryBean– XML-free JPA configuration • LocalContainerEntityManagerFactoryBean has a new property • packagesToScan: analogous to AnnotationSessionFactoryBean
  38. 38. Java Configuration Example Actually: LocalSessionFactoryBuilder
  39. 39. New Testing Features in 3.1– @Configuration class support– Environment profile support– SmartContextLoader– AnnotationConfigContextLoader– DelegatingSmartContextLoader– Updated context cache key generation
  40. 40. SmartContextLoader SPI– Supersedes ContextLoader– Strategy for loading application contexts– From @Configuration classes or resource locations– Supports environment profiles
  41. 41. @ContextConfigurationaccepts a new `classes` attribute...
  42. 42. Ex: @Configuration Test #1
  43. 43. Ex: @Configuration Test #2
  44. 44. Caching
  45. 45. Caching Abstraction– Declarative caching for Spring applications • Minimal impact on code • Plug in various caching solutions
  46. 46. Caching Abstraction– Key annotations • @Cacheable • @CacheEvict
  47. 47. Cache Key– All method arguments used by default– Use SpEL to select more specifically (use class, method, or argument name)
  48. 48. Conditional Caching
  49. 49. Cache Providers (1/2)– Cache and CacheManager SPI • org.springframework.cache– Cache Implementations • EhCacheCache • ConcurrentMapCache and ConcurrentMapCacheFactoryBean
  50. 50. Cache Providers (2/2)– CacheManager Implementations • EhCacheCacheManager • ConcurrentMapCacheManager • SimpleCacheManager • NoOpCacheManager– Any other implementation can be plugged in • GemFire, Coherence, etc.
  51. 51. Cache Configuration– Cache namespace • <cache:annotation-driven /> • “cacheManager” bean
  52. 52. MVC and REST
  53. 53. @MVC 3.0 Config– Built-in defaults • Based on DispatcherServlet.properties– Spring MVC namespace • <mvc:annotation:driven>, <mvc:interceptors>, …
  54. 54. Java-based @MVC 3.1 Config– Most Spring MVC configuration is in Java already • @Controller, @RequestMapping, etc.– Servlet 3.0 enhancements will further reduce the need for web.xml– XML namespace is convenient but … • Not transparent • Not easy to offer the right degree of customization– … What should a Java equivalent to the MVC namespace look like?
  55. 55. @EnableWebMvc– Enables Spring MVC default configuration • Registers components expected by the DispatcherServlet– Allows for configuration similar to the Spring MVC namespace • … and the DispatcherServlet.properties combined
  56. 56. A More Complete Example …– Add component scanning for @Controllers and other beans
  57. 57. Q: Where is the “Enabled” Configuration ?!– A: a framework-provided @Configuration class (actually DelegatingWebMvcConfiguration)
  58. 58. How Do I Customize All This?– Simply implement the WebMvcConfigurer interface Allows selective overriding
  59. 59. HandlerMethod Abstraction– HandlerMethod • A proper abstraction for the selected “handler” in Spring MVC– Not just for @RequestMapping methods • Also @InitBinder, @ModelAttribute, @ExceptionHandler methods– “HandlerMethod” support classes • RequestMappingHandlerMapping • RequestMappingHandlerAdapter • ExceptionHandlerExceptionResolver
  60. 60. Path Variables in the Model– @PathVariable arguments automatically added to the model These can be deleted
  61. 61. URI Templates in Redirect Strings– URL templates supported in “redirect:” strings Expanded from model attributes, which now include @PathVariables
  62. 62. URI Template Vars in Data Binding– URI template variables used in data binding
  63. 63. Matching MediaTypes < @MVC 3.1 – Using the headers condition
  64. 64. Matching MediaTypes in @MVC 3.1 – The consumes and produces conditions If not matched, results in: UNSUPPORTED_MEDIA_TYPE (415) If not matched, results in: NOT_ACCEPTABLE (406)
  65. 65. Servlet 3.0
  66. 66. Servlet 3.0 Containers• Tomcat 7 and GlassFish 3 – Explicitly supported• While preserving compatibility with Servlet 2.4+
  67. 67. XML-free Web-app Config• Support for XML-free web application setup – no web.xml• Made possible via: – Servlet 3.0s ServletContainerInitializer – Spring 3.1s AnnotationConfigWebApplicationContext – Spring 3.1’s environment abstraction
  68. 68. Native Servlet 3.0 in @MVC• Asynchronous request processing• Standard Servlet 3.0 file upload – behind Springs MultipartResolver abstraction
  69. 69. Bonus
  70. 70. "c:" Namespace– Shortcut for <constructor-arg> • inline argument values • analogous to existing "p:" namespace– Use of constructor argument names • recommended for readability • debug symbols have to be available in the applications class files
  71. 71. Spring 3.1 in a Nutshell• Environment and Profiles• Java-based Configuration and @Enable*• Testing with @Configuration and Profiles• Cache Abstraction• MVC and REST Improvements• Servlet 3.0• c: Namespace
  72. 72. Spring 3.2 Roadmap
  73. 73. Build System & Source Control• Moved from Subversion to Git(hub) – Spring 3.1.1 – github.com/SpringSource/spring-framework• Moving from Spring Build (i.e., Ant + Ivy) to Gradle• SPR-8120
  74. 74. Servlet 3.0/3.1 Async Requests• Servlet 3.0/3.1 asynchronous request processing – Explicit support in Spring @MVC• SPR-8517
  75. 75. Java EE 7• JPA 2.1 (JSR-338) – Building on JPA 2.0 support in Spring 3.0 – SPR-8194• JSF 2.2 (JSR-344) – Specification compatibility – SPR-8104• JMS 2.0 (JSR-343) – Long overdue, major overhaul to JMS – Exploring a native JMS API style for use with Spring – SPR-8197
  76. 76. Caching• Support for JCache (JSR-107) compliant cache providers – JCacheCacheManager/Cache adapters for Springs SPI – JCacheManager/JCacheFactoryBean• Support for JCaches standardized caching annotations – ideally automatically through Spring’s <cache:annotation-driven>• SPR-8774
  77. 77. Validation• Bean Validation 1.1 (JSR-349) – Specification compatibility – Building on 1.0 support in Spring 3.0• SPR-8199
  78. 78. Spring Core• Migrate from CGLIB to Javassist – For class-based proxies (AOP) – SPR-8190• Bean definition visibility and overriding – Final bean definitions – Profile-scoped beans – @Bean methods override XML beans – SPR-8189
  79. 79. Spring MVC Testing Support• Unit tests with Mock MVC classes – Simulates the DispatcherServlet• Integration tests with XmlWebApplicationContext – Configured via @ContextConfiguration and other annotations – Loaded via a new ContextLoader
  80. 80. In Closing…
  81. 81. Further Resources• Spring Framework – http://springframework.org – Spring Reference Manual – JavaDoc• Spring Forums – http://forum.springframework.org• Spring JIRA – http://jira.springframework.org
  82. 82. Blogs• SpringSource Team Blog – category 3.1 – http://blog.springsource.com/category/spring/31/• Swiftmind Blog – http://www.swiftmind.com/blog/
  83. 83. Q&ASam Brannentwitter: @sam_brannenwww.slideshare.net/sbrannenwww.swiftmind.com“Spring in a Nutshell” http://oreilly.com/catalog/9780596801946 available from O’Reilly in 2012

×