spring 3.1 in a nutshell - jax london 2011
DESCRIPTION
Covers new features in Spring 3.1 up through RC1.TRANSCRIPT
![Page 1: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/1.jpg)
Spring 3.1 in a Nutshell Sam Brannen / Swiftmind
![Page 2: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/2.jpg)
Speaker Profile
• Spring & Java consultant @ Swiftmind • Developing Java for over 13 years • Spring Framework Core Developer • Spring Trainer • Lead author of “Spring in a Nutshell”
![Page 3: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/3.jpg)
Agenda • Major Themes in 3.x • Environment and Profiles • Java-based Configuration • Testing • Caching • MVC and REST • Servlet 3.0 • Odds & Ends
![Page 4: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/4.jpg)
Major Themes in Spring 3.0
![Page 5: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/5.jpg)
Java-based configuration
![Page 6: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/6.jpg)
custom stereotypes
![Page 7: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/7.jpg)
annotated factory methods
![Page 8: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/8.jpg)
JSR-330 – DI for Java
![Page 9: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/9.jpg)
Spring Expression Language
![Page 10: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/10.jpg)
REST support in Spring MVC
![Page 11: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/11.jpg)
Portlet API 2.0
![Page 12: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/12.jpg)
JSR-303 – bean validation
![Page 13: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/13.jpg)
Java EE 6 support: JPA 2.0, JSF 2.0
![Page 14: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/14.jpg)
– 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
Major Themes in Spring 3.1
![Page 15: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/15.jpg)
Environment and Profiles
![Page 16: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/16.jpg)
Environment Abstraction – Injectable environment abstraction API
• org.springframework.core.env.Environment
– Two core concepts • Property Sources • Bean Profiles
Property Source: A variety of sources: property files, system properties, servlet context, JNDI, etc.
Bean Profile: A logical group of bean definitions. Registered only if the profile is active.
![Page 17: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/17.jpg)
Property Source Abstraction
– Property source
– Property resolution
– Placeholders
![Page 18: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/18.jpg)
PropertySource(s)
– PropertySource • a single property source
– PropertySources • a hierarchy of PropertySource objects • potentially varying across deployment environments
![Page 19: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/19.jpg)
Property Resolution SPI
– org.springframework.core.env.PropertyResolver
– Environment extends PropertyResolver
![Page 20: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/20.jpg)
Custom resolution of placeholders
– dependent on the actual environment
– PropertySourcesPlaceholderConfigurer supersedes PropertyPlaceholderConfigurer
![Page 21: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/21.jpg)
– Stand-alone code
– Web application • Implement ApplicationContextInitializer • Register via contextInitializerClasses context
parameter in web.xml
Managing Property Sources
![Page 22: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/22.jpg)
Accessing Properties
– By injecting the Environment
![Page 23: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/23.jpg)
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
![Page 24: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/24.jpg)
Configuring Profiles in XML
– All bean definitions
– Subset of bean definitions
![Page 25: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/25.jpg)
25
Configuring Profiles in Java Config
– @Profile can also be used on components
• @Component, @Service, @Repository, etc.
![Page 26: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/26.jpg)
Profile Activation Options
– programmatically
– system property
– in web.xml
– in tests via @ActiveProfiles
![Page 27: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/27.jpg)
Activating Profiles… programmatically
![Page 28: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/28.jpg)
Activating Profiles… via system properties
-Dspring.profiles.active=“dev”
-Dspring.profiles.default=“common”
![Page 29: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/29.jpg)
Activating Profiles… in web apps
![Page 30: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/30.jpg)
Activating Profiles… in integration tests
![Page 31: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/31.jpg)
Java-based Configuration
![Page 32: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/32.jpg)
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
![Page 33: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/33.jpg)
Typical Infrastructure Setup
– Transactions
– Scheduling
– MVC customization
– AOP
![Page 34: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/34.jpg)
@Enable* Annotations
– Applied at the class-level on @Configuration classes
– Roughly equivalent to their XML namespace counterparts
![Page 35: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/35.jpg)
@Enable* in 3.1 RC1
– @EnableTransactionManagement – @EnableAsync – @EnableScheduling – @EnableAspectJAutoProxy – @EnableLoadTimeWeaving – @EnableWebMvc
![Page 36: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/36.jpg)
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
![Page 37: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/37.jpg)
Java Configuration Example
Actually: LocalSessionFactoryBuilder
![Page 38: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/38.jpg)
New Testing Features in 3.1
– @Configuration class support – Environment profile support – SmartContextLoader – AnnotationConfigContextLoader – DelegatingSmartContextLoader – Updated context cache key generation
![Page 39: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/39.jpg)
SmartContextLoader SPI
– Supersedes ContextLoader – Strategy for loading application contexts – From @Configuration classes or resource
locations – Supports environment profiles
![Page 40: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/40.jpg)
@ContextConfiguration
accepts a new `classes` attribute...
![Page 41: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/41.jpg)
Ex: @Configuration Test #1
![Page 42: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/42.jpg)
Ex: @Configuration Test #2
![Page 43: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/43.jpg)
Caching
![Page 44: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/44.jpg)
Caching Abstraction
– Declarative caching for Spring applications • Minimal impact on code • Plug in various caching solutions
– Key annotations @Cacheable and
@CacheEvict
![Page 45: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/45.jpg)
Cache Key
– All method arguments used by default
– Use SpEL to select more specifically (use class, method, or argument name)
![Page 46: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/46.jpg)
Conditional Caching
![Page 47: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/47.jpg)
Cache Providers (1/2)
– Cache and CacheManager SPI • org.springframework.cache
– Cache Implementations • EhCacheCache • ConcurrentMapCache and
ConcurrentMapCacheFactoryBean
![Page 48: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/48.jpg)
Cache Providers (2/2)
– CacheManager Implementations • EhCacheCacheManager • ConcurrentMapCacheManager • SimpleCacheManager • NoOpCacheManager
– Any other implementation can be plugged in • GemFire, Coherence, etc.
![Page 49: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/49.jpg)
Cache Configuration
– Cache namespace • <cache:annotation-driven /> • “cacheManager” bean
![Page 50: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/50.jpg)
MVC and REST
![Page 51: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/51.jpg)
@MVC 3.0 Config – Built-in defaults
• Based on DispatcherServlet.properties
– Spring MVC namespace • <mvc:annotation:driven>, <mvc:interceptors>, …
![Page 52: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/52.jpg)
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?
![Page 53: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/53.jpg)
@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
![Page 54: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/54.jpg)
A More Complete Example … – Add component scanning for @Controllers
and other beans
![Page 55: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/55.jpg)
Q: Where is the “Enabled” Configuration ?!
– A: a framework-provided @Configuration class (actually DelegatingWebMvcConfiguration)
![Page 56: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/56.jpg)
How Do I Customize All This?
– Simply implement the WebMvcConfigurer interface Allows selective overriding
![Page 57: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/57.jpg)
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
![Page 58: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/58.jpg)
Path Variables in the Model
– @PathVariable arguments automatically added to the model
These can be deleted
![Page 59: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/59.jpg)
URI Templates in Redirect Strings
– URL templates supported in “redirect:” strings
Expanded from model attributes, which now include @PathVariables
![Page 60: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/60.jpg)
URI Template Variables in Data Binding
– URI template variables used in data binding
![Page 61: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/61.jpg)
Matching MediaTypes < @MVC 3.1
– Using the 'headers' condition
![Page 62: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/62.jpg)
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)
![Page 63: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/63.jpg)
Servlet 3.0
![Page 64: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/64.jpg)
Servlet 3.0 Containers
• Tomcat 7 and GlassFish 3 – Explicitly supported
• While preserving compatibility with Servlet 2.4+
![Page 65: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/65.jpg)
XML-free Web-app Config
• Support for XML-free web application setup – no web.xml
• Made possible via: – Servlet 3.0's ServletContainerInitializer – Spring 3.1's
AnnotationConfigWebApplicationContext – Spring 3.1’s environment abstraction
![Page 66: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/66.jpg)
Native Servlet 3.0 in @MVC
• Asynchronous request processing
• Standard Servlet 3.0 file upload – behind Spring's MultipartResolver abstraction
![Page 67: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/67.jpg)
Odds & Ends
![Page 68: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/68.jpg)
"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
application's class files
![Page 69: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/69.jpg)
The Spring Roadmap
• Spring 3.1 RC2: mid November
• Spring 3.1 GA: Before end of 2011
• Spring 3.2: Planned for early 2012 – Java EE: JSF 2.2, JPA 2.1, etc.
![Page 70: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/70.jpg)
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
![Page 71: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/71.jpg)
Further Resources
• Spring Framework – http://springframework.org – Spring Reference Manual – JavaDoc
• Spring Forums – http://forum.springframework.org
• Spring JIRA – http://jira.springframework.org
![Page 72: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/72.jpg)
Blogs
• SpringSource Team Blog – category 3.1 – http://blog.springsource.com/category/spring/31/
• Swiftmind Blog – http://www.swiftmind.com/blog/
![Page 73: Spring 3.1 in a Nutshell - JAX London 2011](https://reader034.vdocuments.site/reader034/viewer/2022052410/554f5c97b4c905524c8b558a/html5/thumbnails/73.jpg)
Sam Brannen twitter: @sam_brannen Swiftmind www.slideshare.net/sbrannen www.swiftmind.com “Spring in a Nutshell” http://oreilly.com/catalog/9780596801946 available from O’Reilly in 2012
Q&A