SlideShare a Scribd company logo
1 of 52
Download to read offline
Spring 3.1 in a Nutshell



Sam Brannen
Swiftmind
Java Track 12.2
Talk #394
Speaker Profile

>    Java developer with 13+ years' experience
>    Spring Framework Core Developer
>    Lead author of Spring in a Nutshell
>    Previous SpringSource dm Server™ developer
>    Presenter on Spring, Java, OSGi, and testing
>    Senior Software Consultant @ Swiftmind




                                                    2
Agenda

>    Major Themes in 3.x
>    Environment and Profiles
>    Java-based Configuration
>    Testing
>    Caching
>    MVC and REST
>    Servlet 3.0
>    Odds & Ends


                                3
Major Themes in Spring 3.x




                             4
Spring Framework 3.0: A Quick Review


>  Annotation-based component model
   –  Stereotypes, autowiring, factory methods, JSR-330
>  Spring Expression Language
   –  Unified EL++
>  REST support in Spring MVC
   –  Various @MVC programming model improvements
>  Portlet API 2.0
   –  Event/Resource requests
>  Declarative model validation
   –  JSR-303 bean validation
>  Java EE 6 support
   –  JPA 2.0, JSF 2.0 but also JSR-330, JSR-303          5
Spring Framework 3.1: Major Themes


>  Environment Abstraction
   –  PropertySources, Bean Profiles, TestContext support
>  Java-based Application Configuration
   –  Equivalents for XML namespaces, FactoryBeans, TestContext support
>  High-level Cache API
   –  Transparent use of various caching solutions
>  Customizable @MVC Processing
   –  New infrastructure for annotated controllers
>  Explicit Support for Servlet 3.0
   –  ServletContainerInitializer and MultipartResolver


                                                                          6
Environment and Profiles




                           7
Environment Abstraction


>  Injectable environment abstraction API
   –  org.springframework.core.env.Environment


>  Two core concepts
   –  Property Sources
   –  Bean Profiles


    Property Source:                     Bean Profile:
    A variety of sources: property       A logical group of bean
    files, system properties, servlet    definitions. Registered only if
    context, JNDI, etc.                  the profile is active.

                                                                           8
Property Source Abstraction


>  Property resolution SPI
   –  org.springframework.core.env.PropertyResolver
   –  Environment extends PropertyResolver
>  PropertySource
   –  a single property source
>  PropertySources
   –  a hierarchy of PropertySource objects
   –  potentially varying across deployment environments
>  Custom resolution of placeholders
   –  dependent on the actual environment
   –  PropertySourcesPlaceholderConfigurer supersedes
      PropertyPlaceholderConfigurer
                                                           9
Managing Property Sources


>  Standalone code




>  Web application
   –  Implement ApplicationContextInitializer
   –  Register via contextInitializerClasses context parameter in web.xml

                                                                            10
Accessing Properties


>  By injecting the Environment




                                  11
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
>  Activation
   –  programmatically
   –  in web.xml
   –  system property
   –  in tests via @ActiveProfiles              12
Configuring Profiles in XML


>  All bean definitions




>  Subset of bean definitions




                                13
Configuring Profiles in Java Config




>  @Profile can also be used on components
   –  @Component, @Service, @Repository, etc.   14
Activating Profiles (1/2)


>  Standalone code




>  In Web applications




                            15
Activating Profiles (2/2)


>  Via Java system properties
   –  -Dspring.profiles.active=“dev”
   –  -Dspring.profiles.default=“common”


>  With the Spring TestContext Framework




                                           16
Java-based Configuration




                           17
Java Configuration Enhancements


>  Java-based equivalent to mechanisms available in XML
   –  XML namespaces  @Enable*
   –  FactoryBeans  builders
   –  GenericXmlContextLoader  AnnotationConfigContextLoader
>  Not a one-on-one mapping
   –  Make the most of what Java has to offer
   –  Intuitive annotation-oriented container configuration
>  Typical infrastructure setup
   –  transactions
   –  scheduling
   –  MVC customization
                                                                18
@Enable* Annotations


>  Applied at the class-level on @Configuration classes


>  Roughly equivalent to their XML namespace counterparts


>  Available in Spring 3.1 M2:
   –  @EnableTransactionManagement
   –  @EnableAsync
   –  @EnableScheduling
   –  @EnableLoadTimeWeaving
   –  @EnableWebMvc


                                                            19
Hibernate and JPA


>  Hibernate SessionFactory builder APIs
   –  SessionFactoryBuilder
   –  AnnotationSessionFactoryBuilder


>  XML-free JPA configuration
   –  LocalContainerEntityManagerFactoryBean has a new property
   –  packagesToScan: analogous to AnnotationSessionFactoryBean




                                                                  20
Java Configuration Example




                             21
Testing with @Configuration Classes


>  @ActiveProfiles
   –  declares active profiles for test
>  @ContextConfiguration supports a new classes attribute
   –  Not supported by existing ContextLoader SPI
>  SmartContextLoader supersedes ContextLoader
   –  can process resource locations and configuration classes
   –  can set active bean definition profiles
>  AnnotationConfigContextLoader
   –  SmartContextLoader that supports @Configuration classes
   –  convention over configuration: static inner class
>  Context cache key generation
   –  updated to take locations, classes, profiles, and loader into account   22
@Configuration Testing Example




                                 23
Caching




          24
Caching Abstraction


>  Declarative caching for Spring applications
   –  Minimal impact on code
   –  Plug in various caching solutions

>  Key annotations @Cacheable and @CacheEvict




                                                 25
@Cacheable Options


>  The cache key
   –  All method arguments used by default




   –  Use SpEL to select more specifically (use class, method, or argument name)




>  Conditional caching

                                                                              26
Cache Providers


>  Cache and CacheManager SPI
   –  org.springframework.cache


>  Cache Implementations
   –  EhCacheCache
   –  ConcurrentMapCache and ConcurrentMapCacheFactoryBean
>  CacheManager Implementations
   –  EhCacheCacheManager
   –  SimpleCacheManager


>  Any other implementation can be plugged in
   –  GemFire, Coherence, etc.                               27
Cache Configuration


>  Cache namespace
   –  <cache:annotation-driven />
   –  “cacheManager” bean




                                    28
MVC and REST




               29
How We Configure Spring MVC Today


>  Built-in defaults
   –  Based on DispatcherServlet.properties

>  Spring MVC namespace
   –  <mvc:annotation:driven>, <mvc:interceptors>, …




                                                       30
Why Java-based Configuration For Spring MVC?


>  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?
                                                                       31
Java-based Configuration With @EnableWebMvc


>  Adding it enables Spring MVC default configuration
   –  Registers components expected by the DispatcherServlet

>  Provides configuration similar to the Spring MVC namespace
   –  … and the DispatcherServlet.properties combined




                                                                32
A More Complete Example …


>  Add component scanning for @Controllers and other beans




                                                             33
Q: Where Is The “Enabled” Configuration ?!


>  A: a framework-provided @Configuration class (actually
   DelegatingWebMvcConfiguration)




                                                            34
How Do I Customize All This?


>  Simply implement the WebMvcConfigurer interface
                                                     Allows selective overriding




                                                                              35
Updated @MVC Support


>  HandlerMethod
   –  A proper abstraction for the selected “handler” in Spring MVC

>  Not just for @RequestMapping methods
   –  Also @InitBinder, @ModelAttribute, @ExceptionHandler methods
   –  Not limited to the above

>  “HandlerMethod” support classes
   –  RequestMappingHandlerMapping
   –  RequestMappingHandlerAdapter
   –  ExceptionHandlerExceptionResolver

                                                                      36
Configuring the New @MVC Support Classes


>  Enabled by default
   –  XML namespace … <mvc:annotation-driven />
   –  Java-based configuration … @EnableWebMvc

>  Existing support classes continue to exist
   –  No new functionally will be developed for them

>  But the new support classes are recommended
   –  They are generally functionally equivalent




                                                       37
Path Variables in The Model


>  @PathVariable arguments automatically added to the model




                                                 These can be deleted



                                                                        38
URI Templates in Redirect Strings


>  URL templates supported in “redirect:” strings




                                 Expanded from model attributes,
                                which now include @PathVariables
                                                                   39
URI Template Variables in Data Binding


>  URI template variables used in data binding




                                                 40
Matching MediaTypes before Spring MVC 3.1


>  Using the 'headers' condition




                                            41
Matching MediaTypes in Spring 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)
                                                                  42
Servlet 3.0




              43
Support for Servlet 3.0


>  Explicit support for Servlet 3.0 containers
   –  Tomcat 7 and GlassFish 3
   –  while preserving compatibility with Servlet 2.4+


>  Support for XML-free web application setup (no web.xml)
   –  Servlet 3.0's ServletContainerInitializer plus Spring 3.1's
      AnnotationConfigWebApplicationContext plus the environment abstraction


>  Exposure of native Servlet 3.0 functionality in Spring MVC
   –  support for asynchronous request processing
   –  standard Servlet 3.0 file upload support behind Spring's MultipartResolver
      abstraction
                                                                                   44
WebApplicationInitializer Example




                                    45
Odds & Ends




              46
"c:" Namespace for XML Configuration


>  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




                                                                            47
The Spring Roadmap


>  Spring 3.1 M2:     June 9, 2011


>  Spring 3.1 RC1:    Coming soon! (no M3 planned)


>  Spring 3.1 GA:     Soon after RC1


>  Spring 3.2:        Planned for early 2012
   –  Java SE 7
   –  JDBC 4.1
   –  Fork-join framework
   –  Java EE: JSF 2.2, JPA 2.1
                                                     48
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




                                                49
Further Resources


>  Spring Framework
   –  http://springframework.org
   –  Spring Reference Manual
   –  JavaDoc
>  Spring Forums
   –  http://forum.springframework.org
>  Spring JIRA
   –  http://jira.springframework.org
>  SpringSource Team Blog – category 3.1
   –  http://blog.springsource.com/category/spring/31/
>  Swiftmind Blog
   –  http://www.swiftmind.com/blog/                     50
Special Thanks to…


>  Jürgen Höller, Chris Beams, and Rossen Stoyanchev of SpringSource
   –  for donating examples and content to make this presentation possible




                                                                             51
Q&A
Sam Brannen                         sam.brannen@swiftmind.com
Swiftmind                           twitter: @sam_brannen
                                    www.slideshare.net/sbrannen
                                    www.swiftmind.com



“Spring in a Nutshell”
   http://oreilly.com/catalog/9780596801946
   available from O’Reilly in early 2012

More Related Content

What's hot

Lecture 5 JSTL, custom tags, maven
Lecture 5   JSTL, custom tags, mavenLecture 5   JSTL, custom tags, maven
Lecture 5 JSTL, custom tags, mavenFahad Golra
 
Ejb 3.0 Runtime Environment
Ejb 3.0 Runtime EnvironmentEjb 3.0 Runtime Environment
Ejb 3.0 Runtime Environmentrradhak
 
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 javatwo2011
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentationsourabh aggarwal
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkRajind Ruparathna
 
Java Web Programming [6/9] : MVC
Java Web Programming [6/9] : MVCJava Web Programming [6/9] : MVC
Java Web Programming [6/9] : MVCIMC Institute
 
Spring Framework Petclinic sample application
Spring Framework Petclinic sample applicationSpring Framework Petclinic sample application
Spring Framework Petclinic sample applicationAntoine Rey
 
Spring training
Spring trainingSpring training
Spring trainingshah_d_p
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204ealio
 
Spring 4 advanced final_xtr_presentation
Spring 4 advanced final_xtr_presentationSpring 4 advanced final_xtr_presentation
Spring 4 advanced final_xtr_presentationsourabh aggarwal
 
Introduction to java servlet 3.0 api javaone 2009
Introduction to java servlet 3.0 api javaone 2009Introduction to java servlet 3.0 api javaone 2009
Introduction to java servlet 3.0 api javaone 2009JavaEE Trainers
 
OpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheConOpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheConos890
 
Arun Gupta: London Java Community: Java EE 6 and GlassFish 3
Arun Gupta: London Java Community: Java EE 6 and GlassFish 3 Arun Gupta: London Java Community: Java EE 6 and GlassFish 3
Arun Gupta: London Java Community: Java EE 6 and GlassFish 3 Skills Matter
 
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...탑크리에듀(구로디지털단지역3번출구 2분거리)
 
Understanding
Understanding Understanding
Understanding Arun Gupta
 

What's hot (20)

Lecture 5 JSTL, custom tags, maven
Lecture 5   JSTL, custom tags, mavenLecture 5   JSTL, custom tags, maven
Lecture 5 JSTL, custom tags, maven
 
Javaee6 Overview
Javaee6 OverviewJavaee6 Overview
Javaee6 Overview
 
Ejb 3.0 Runtime Environment
Ejb 3.0 Runtime EnvironmentEjb 3.0 Runtime Environment
Ejb 3.0 Runtime Environment
 
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentation
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Java Web Programming [6/9] : MVC
Java Web Programming [6/9] : MVCJava Web Programming [6/9] : MVC
Java Web Programming [6/9] : MVC
 
Spring Framework Petclinic sample application
Spring Framework Petclinic sample applicationSpring Framework Petclinic sample application
Spring Framework Petclinic sample application
 
Spring frame work
Spring frame workSpring frame work
Spring frame work
 
Spring training
Spring trainingSpring training
Spring training
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
Spring 4 advanced final_xtr_presentation
Spring 4 advanced final_xtr_presentationSpring 4 advanced final_xtr_presentation
Spring 4 advanced final_xtr_presentation
 
Introduction to java servlet 3.0 api javaone 2009
Introduction to java servlet 3.0 api javaone 2009Introduction to java servlet 3.0 api javaone 2009
Introduction to java servlet 3.0 api javaone 2009
 
Advance Java
Advance JavaAdvance Java
Advance Java
 
OpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheConOpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheCon
 
Arun Gupta: London Java Community: Java EE 6 and GlassFish 3
Arun Gupta: London Java Community: Java EE 6 and GlassFish 3 Arun Gupta: London Java Community: Java EE 6 and GlassFish 3
Arun Gupta: London Java Community: Java EE 6 and GlassFish 3
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
[스프링/Spring교육학원,자바교육,근로자교육,실업자교육추천학원_탑크리에듀]#6.스프링프레임워크 & 마이바티스 (Spring Framew...
 
Understanding
Understanding Understanding
Understanding
 

Similar to Spring 3.1 in a Nutshell

Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012Sam Brannen
 
Spring 3.1 in a Nutshell - JAX London 2011
Spring 3.1 in a Nutshell - JAX London 2011Spring 3.1 in a Nutshell - JAX London 2011
Spring 3.1 in a Nutshell - JAX London 2011Sam Brannen
 
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam BrannenSpring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam BrannenJAX London
 
Spring 3.1: a Walking Tour
Spring 3.1: a Walking TourSpring 3.1: a Walking Tour
Spring 3.1: a Walking TourJoshua Long
 
Testing Web Apps with Spring Framework 3.2
Testing Web Apps with Spring Framework 3.2Testing Web Apps with Spring Framework 3.2
Testing Web Apps with Spring Framework 3.2Rossen Stoyanchev
 
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Arun Gupta
 
JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0Arun Gupta
 
Spring design-juergen-qcon
Spring design-juergen-qconSpring design-juergen-qcon
Spring design-juergen-qconYiwei Ma
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Tuna Tore
 
springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892Tuna Tore
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Arun Gupta
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2javatrainingonline
 
Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference O...
Java EE 6 = Less Code + More Power (Tutorial)  [5th IndicThreads Conference O...Java EE 6 = Less Code + More Power (Tutorial)  [5th IndicThreads Conference O...
Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference O...IndicThreads
 
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010Jagadish Prasath
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depthVinay Kumar
 

Similar to Spring 3.1 in a Nutshell (20)

Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
Spring 3.1 to 3.2 in a Nutshell - Spring I/O 2012
 
Spring 3.1 in a Nutshell - JAX London 2011
Spring 3.1 in a Nutshell - JAX London 2011Spring 3.1 in a Nutshell - JAX London 2011
Spring 3.1 in a Nutshell - JAX London 2011
 
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam BrannenSpring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
 
Spring 3.1: a Walking Tour
Spring 3.1: a Walking TourSpring 3.1: a Walking Tour
Spring 3.1: a Walking Tour
 
Testing Web Apps with Spring Framework 3.2
Testing Web Apps with Spring Framework 3.2Testing Web Apps with Spring Framework 3.2
Testing Web Apps with Spring Framework 3.2
 
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
Servlets 3.0 - Asynchronous, Easy, Extensible @ Silicon Valley Code Camp 2010
 
JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0
 
Lo nuevo en Spring 3.0
Lo nuevo  en Spring 3.0Lo nuevo  en Spring 3.0
Lo nuevo en Spring 3.0
 
Spring WebApplication development
Spring WebApplication developmentSpring WebApplication development
Spring WebApplication development
 
Spring design-juergen-qcon
Spring design-juergen-qconSpring design-juergen-qcon
Spring design-juergen-qcon
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5
 
springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
 
Next stop: Spring 4
Next stop: Spring 4Next stop: Spring 4
Next stop: Spring 4
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2
 
Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2
 
Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference O...
Java EE 6 = Less Code + More Power (Tutorial)  [5th IndicThreads Conference O...Java EE 6 = Less Code + More Power (Tutorial)  [5th IndicThreads Conference O...
Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference O...
 
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
 

More from Sam Brannen

Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023Sam Brannen
 
Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022Sam Brannen
 
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019Sam Brannen
 
JUnit 5: What's New and What's Coming - Spring I/O 2019
JUnit 5: What's New and What's Coming - Spring I/O 2019JUnit 5: What's New and What's Coming - Spring I/O 2019
JUnit 5: What's New and What's Coming - Spring I/O 2019Sam Brannen
 
JUnit 5 - New Opportunities for Testing on the JVM
JUnit 5 - New Opportunities for Testing on the JVMJUnit 5 - New Opportunities for Testing on the JVM
JUnit 5 - New Opportunities for Testing on the JVMSam Brannen
 
Get the Most out of Testing with Spring 4.2
Get the Most out of Testing with Spring 4.2Get the Most out of Testing with Spring 4.2
Get the Most out of Testing with Spring 4.2Sam Brannen
 
JUnit 5 - from Lambda to Alpha and beyond
JUnit 5 - from Lambda to Alpha and beyondJUnit 5 - from Lambda to Alpha and beyond
JUnit 5 - from Lambda to Alpha and beyondSam Brannen
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An IntroductionSam Brannen
 
Testing with Spring 4.x
Testing with Spring 4.xTesting with Spring 4.x
Testing with Spring 4.xSam Brannen
 
Spring Framework 4.1
Spring Framework 4.1Spring Framework 4.1
Spring Framework 4.1Sam Brannen
 
Testing Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsTesting Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsSam Brannen
 
Composable Software Architecture with Spring
Composable Software Architecture with SpringComposable Software Architecture with Spring
Composable Software Architecture with SpringSam Brannen
 
Spring Framework 4.0 to 4.1
Spring Framework 4.0 to 4.1Spring Framework 4.0 to 4.1
Spring Framework 4.0 to 4.1Sam Brannen
 
Spring Framework 4.0 - The Next Generation - Soft-Shake 2013
Spring Framework 4.0 - The Next Generation - Soft-Shake 2013Spring Framework 4.0 - The Next Generation - Soft-Shake 2013
Spring Framework 4.0 - The Next Generation - Soft-Shake 2013Sam Brannen
 
Spring Framework 3.2 - What's New
Spring Framework 3.2 - What's NewSpring Framework 3.2 - What's New
Spring Framework 3.2 - What's NewSam Brannen
 
Spring 3.1 and MVC Testing Support - 4Developers
Spring 3.1 and MVC Testing Support - 4DevelopersSpring 3.1 and MVC Testing Support - 4Developers
Spring 3.1 and MVC Testing Support - 4DevelopersSam Brannen
 
Effective out-of-container Integration Testing - 4Developers
Effective out-of-container Integration Testing - 4DevelopersEffective out-of-container Integration Testing - 4Developers
Effective out-of-container Integration Testing - 4DevelopersSam Brannen
 
Spring 3.1 to 3.2 in a Nutshell - SDC2012
Spring 3.1 to 3.2 in a Nutshell - SDC2012Spring 3.1 to 3.2 in a Nutshell - SDC2012
Spring 3.1 to 3.2 in a Nutshell - SDC2012Sam Brannen
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSam Brannen
 
Effective out-of-container Integration Testing
Effective out-of-container Integration TestingEffective out-of-container Integration Testing
Effective out-of-container Integration TestingSam Brannen
 

More from Sam Brannen (20)

Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
 
Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022
 
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
JUnit 5 - Evolution and Innovation - SpringOne Platform 2019
 
JUnit 5: What's New and What's Coming - Spring I/O 2019
JUnit 5: What's New and What's Coming - Spring I/O 2019JUnit 5: What's New and What's Coming - Spring I/O 2019
JUnit 5: What's New and What's Coming - Spring I/O 2019
 
JUnit 5 - New Opportunities for Testing on the JVM
JUnit 5 - New Opportunities for Testing on the JVMJUnit 5 - New Opportunities for Testing on the JVM
JUnit 5 - New Opportunities for Testing on the JVM
 
Get the Most out of Testing with Spring 4.2
Get the Most out of Testing with Spring 4.2Get the Most out of Testing with Spring 4.2
Get the Most out of Testing with Spring 4.2
 
JUnit 5 - from Lambda to Alpha and beyond
JUnit 5 - from Lambda to Alpha and beyondJUnit 5 - from Lambda to Alpha and beyond
JUnit 5 - from Lambda to Alpha and beyond
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An Introduction
 
Testing with Spring 4.x
Testing with Spring 4.xTesting with Spring 4.x
Testing with Spring 4.x
 
Spring Framework 4.1
Spring Framework 4.1Spring Framework 4.1
Spring Framework 4.1
 
Testing Spring MVC and REST Web Applications
Testing Spring MVC and REST Web ApplicationsTesting Spring MVC and REST Web Applications
Testing Spring MVC and REST Web Applications
 
Composable Software Architecture with Spring
Composable Software Architecture with SpringComposable Software Architecture with Spring
Composable Software Architecture with Spring
 
Spring Framework 4.0 to 4.1
Spring Framework 4.0 to 4.1Spring Framework 4.0 to 4.1
Spring Framework 4.0 to 4.1
 
Spring Framework 4.0 - The Next Generation - Soft-Shake 2013
Spring Framework 4.0 - The Next Generation - Soft-Shake 2013Spring Framework 4.0 - The Next Generation - Soft-Shake 2013
Spring Framework 4.0 - The Next Generation - Soft-Shake 2013
 
Spring Framework 3.2 - What's New
Spring Framework 3.2 - What's NewSpring Framework 3.2 - What's New
Spring Framework 3.2 - What's New
 
Spring 3.1 and MVC Testing Support - 4Developers
Spring 3.1 and MVC Testing Support - 4DevelopersSpring 3.1 and MVC Testing Support - 4Developers
Spring 3.1 and MVC Testing Support - 4Developers
 
Effective out-of-container Integration Testing - 4Developers
Effective out-of-container Integration Testing - 4DevelopersEffective out-of-container Integration Testing - 4Developers
Effective out-of-container Integration Testing - 4Developers
 
Spring 3.1 to 3.2 in a Nutshell - SDC2012
Spring 3.1 to 3.2 in a Nutshell - SDC2012Spring 3.1 to 3.2 in a Nutshell - SDC2012
Spring 3.1 to 3.2 in a Nutshell - SDC2012
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Effective out-of-container Integration Testing
Effective out-of-container Integration TestingEffective out-of-container Integration Testing
Effective out-of-container Integration Testing
 

Spring 3.1 in a Nutshell

  • 1. Spring 3.1 in a Nutshell Sam Brannen Swiftmind Java Track 12.2 Talk #394
  • 2. Speaker Profile >  Java developer with 13+ years' experience >  Spring Framework Core Developer >  Lead author of Spring in a Nutshell >  Previous SpringSource dm Server™ developer >  Presenter on Spring, Java, OSGi, and testing >  Senior Software Consultant @ Swiftmind 2
  • 3. Agenda >  Major Themes in 3.x >  Environment and Profiles >  Java-based Configuration >  Testing >  Caching >  MVC and REST >  Servlet 3.0 >  Odds & Ends 3
  • 4. Major Themes in Spring 3.x 4
  • 5. Spring Framework 3.0: A Quick Review >  Annotation-based component model –  Stereotypes, autowiring, factory methods, JSR-330 >  Spring Expression Language –  Unified EL++ >  REST support in Spring MVC –  Various @MVC programming model improvements >  Portlet API 2.0 –  Event/Resource requests >  Declarative model validation –  JSR-303 bean validation >  Java EE 6 support –  JPA 2.0, JSF 2.0 but also JSR-330, JSR-303 5
  • 6. Spring Framework 3.1: Major Themes >  Environment Abstraction –  PropertySources, Bean Profiles, TestContext support >  Java-based Application Configuration –  Equivalents for XML namespaces, FactoryBeans, TestContext support >  High-level Cache API –  Transparent use of various caching solutions >  Customizable @MVC Processing –  New infrastructure for annotated controllers >  Explicit Support for Servlet 3.0 –  ServletContainerInitializer and MultipartResolver 6
  • 8. Environment Abstraction >  Injectable environment abstraction API –  org.springframework.core.env.Environment >  Two core concepts –  Property Sources –  Bean Profiles Property Source: Bean Profile: A variety of sources: property A logical group of bean files, system properties, servlet definitions. Registered only if context, JNDI, etc. the profile is active. 8
  • 9. Property Source Abstraction >  Property resolution SPI –  org.springframework.core.env.PropertyResolver –  Environment extends PropertyResolver >  PropertySource –  a single property source >  PropertySources –  a hierarchy of PropertySource objects –  potentially varying across deployment environments >  Custom resolution of placeholders –  dependent on the actual environment –  PropertySourcesPlaceholderConfigurer supersedes PropertyPlaceholderConfigurer 9
  • 10. Managing Property Sources >  Standalone code >  Web application –  Implement ApplicationContextInitializer –  Register via contextInitializerClasses context parameter in web.xml 10
  • 11. Accessing Properties >  By injecting the Environment 11
  • 12. 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 >  Activation –  programmatically –  in web.xml –  system property –  in tests via @ActiveProfiles 12
  • 13. Configuring Profiles in XML >  All bean definitions >  Subset of bean definitions 13
  • 14. Configuring Profiles in Java Config >  @Profile can also be used on components –  @Component, @Service, @Repository, etc. 14
  • 15. Activating Profiles (1/2) >  Standalone code >  In Web applications 15
  • 16. Activating Profiles (2/2) >  Via Java system properties –  -Dspring.profiles.active=“dev” –  -Dspring.profiles.default=“common” >  With the Spring TestContext Framework 16
  • 18. Java Configuration Enhancements >  Java-based equivalent to mechanisms available in XML –  XML namespaces  @Enable* –  FactoryBeans  builders –  GenericXmlContextLoader  AnnotationConfigContextLoader >  Not a one-on-one mapping –  Make the most of what Java has to offer –  Intuitive annotation-oriented container configuration >  Typical infrastructure setup –  transactions –  scheduling –  MVC customization 18
  • 19. @Enable* Annotations >  Applied at the class-level on @Configuration classes >  Roughly equivalent to their XML namespace counterparts >  Available in Spring 3.1 M2: –  @EnableTransactionManagement –  @EnableAsync –  @EnableScheduling –  @EnableLoadTimeWeaving –  @EnableWebMvc 19
  • 20. Hibernate and JPA >  Hibernate SessionFactory builder APIs –  SessionFactoryBuilder –  AnnotationSessionFactoryBuilder >  XML-free JPA configuration –  LocalContainerEntityManagerFactoryBean has a new property –  packagesToScan: analogous to AnnotationSessionFactoryBean 20
  • 22. Testing with @Configuration Classes >  @ActiveProfiles –  declares active profiles for test >  @ContextConfiguration supports a new classes attribute –  Not supported by existing ContextLoader SPI >  SmartContextLoader supersedes ContextLoader –  can process resource locations and configuration classes –  can set active bean definition profiles >  AnnotationConfigContextLoader –  SmartContextLoader that supports @Configuration classes –  convention over configuration: static inner class >  Context cache key generation –  updated to take locations, classes, profiles, and loader into account 22
  • 24. Caching 24
  • 25. Caching Abstraction >  Declarative caching for Spring applications –  Minimal impact on code –  Plug in various caching solutions >  Key annotations @Cacheable and @CacheEvict 25
  • 26. @Cacheable Options >  The cache key –  All method arguments used by default –  Use SpEL to select more specifically (use class, method, or argument name) >  Conditional caching 26
  • 27. Cache Providers >  Cache and CacheManager SPI –  org.springframework.cache >  Cache Implementations –  EhCacheCache –  ConcurrentMapCache and ConcurrentMapCacheFactoryBean >  CacheManager Implementations –  EhCacheCacheManager –  SimpleCacheManager >  Any other implementation can be plugged in –  GemFire, Coherence, etc. 27
  • 28. Cache Configuration >  Cache namespace –  <cache:annotation-driven /> –  “cacheManager” bean 28
  • 30. How We Configure Spring MVC Today >  Built-in defaults –  Based on DispatcherServlet.properties >  Spring MVC namespace –  <mvc:annotation:driven>, <mvc:interceptors>, … 30
  • 31. Why Java-based Configuration For Spring MVC? >  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? 31
  • 32. Java-based Configuration With @EnableWebMvc >  Adding it enables Spring MVC default configuration –  Registers components expected by the DispatcherServlet >  Provides configuration similar to the Spring MVC namespace –  … and the DispatcherServlet.properties combined 32
  • 33. A More Complete Example … >  Add component scanning for @Controllers and other beans 33
  • 34. Q: Where Is The “Enabled” Configuration ?! >  A: a framework-provided @Configuration class (actually DelegatingWebMvcConfiguration) 34
  • 35. How Do I Customize All This? >  Simply implement the WebMvcConfigurer interface Allows selective overriding 35
  • 36. Updated @MVC Support >  HandlerMethod –  A proper abstraction for the selected “handler” in Spring MVC >  Not just for @RequestMapping methods –  Also @InitBinder, @ModelAttribute, @ExceptionHandler methods –  Not limited to the above >  “HandlerMethod” support classes –  RequestMappingHandlerMapping –  RequestMappingHandlerAdapter –  ExceptionHandlerExceptionResolver 36
  • 37. Configuring the New @MVC Support Classes >  Enabled by default –  XML namespace … <mvc:annotation-driven /> –  Java-based configuration … @EnableWebMvc >  Existing support classes continue to exist –  No new functionally will be developed for them >  But the new support classes are recommended –  They are generally functionally equivalent 37
  • 38. Path Variables in The Model >  @PathVariable arguments automatically added to the model These can be deleted 38
  • 39. URI Templates in Redirect Strings >  URL templates supported in “redirect:” strings Expanded from model attributes, which now include @PathVariables 39
  • 40. URI Template Variables in Data Binding >  URI template variables used in data binding 40
  • 41. Matching MediaTypes before Spring MVC 3.1 >  Using the 'headers' condition 41
  • 42. Matching MediaTypes in Spring 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) 42
  • 44. Support for Servlet 3.0 >  Explicit support for Servlet 3.0 containers –  Tomcat 7 and GlassFish 3 –  while preserving compatibility with Servlet 2.4+ >  Support for XML-free web application setup (no web.xml) –  Servlet 3.0's ServletContainerInitializer plus Spring 3.1's AnnotationConfigWebApplicationContext plus the environment abstraction >  Exposure of native Servlet 3.0 functionality in Spring MVC –  support for asynchronous request processing –  standard Servlet 3.0 file upload support behind Spring's MultipartResolver abstraction 44
  • 47. "c:" Namespace for XML Configuration >  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 47
  • 48. The Spring Roadmap >  Spring 3.1 M2: June 9, 2011 >  Spring 3.1 RC1: Coming soon! (no M3 planned) >  Spring 3.1 GA: Soon after RC1 >  Spring 3.2: Planned for early 2012 –  Java SE 7 –  JDBC 4.1 –  Fork-join framework –  Java EE: JSF 2.2, JPA 2.1 48
  • 49. 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 49
  • 50. Further Resources >  Spring Framework –  http://springframework.org –  Spring Reference Manual –  JavaDoc >  Spring Forums –  http://forum.springframework.org >  Spring JIRA –  http://jira.springframework.org >  SpringSource Team Blog – category 3.1 –  http://blog.springsource.com/category/spring/31/ >  Swiftmind Blog –  http://www.swiftmind.com/blog/ 50
  • 51. Special Thanks to… >  Jürgen Höller, Chris Beams, and Rossen Stoyanchev of SpringSource –  for donating examples and content to make this presentation possible 51
  • 52. Q&A Sam Brannen sam.brannen@swiftmind.com Swiftmind twitter: @sam_brannen www.slideshare.net/sbrannen www.swiftmind.com “Spring in a Nutshell” http://oreilly.com/catalog/9780596801946 available from O’Reilly in early 2012