spring 3.1 in a nutshell

52
Spring 3.1 in a Nutshell Sam Brannen Swiftmind Java Track 12.2 Talk #394

Upload: sam-brannen

Post on 10-May-2015

13.402 views

Category:

Documents


1 download

DESCRIPTION

Spring 3.1 introduces several eagerly awaited features including bean definition profiles (a.k.a., environment-specific beans), enhanced Java-based application and infrastructure configuration (a la XML namespaces), a new cache abstraction, and MVC improvements. This session will provide attendees an in-depth look at these major new features as well as an overview of additional enhancements to the framework such as the new c: namespace for constructor arguments, updates regarding Servlet 3.0, and improvements to Spring's integration testing support.

TRANSCRIPT

Page 1: Spring 3.1 in a Nutshell

Spring 3.1 in a Nutshell

Sam Brannen Swiftmind Java Track 12.2

Talk #394

Page 2: Spring 3.1 in a Nutshell

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

Page 3: Spring 3.1 in a Nutshell

3

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

Major Themes in Spring 3.x

4

Page 5: Spring 3.1 in a Nutshell

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

Page 6: Spring 3.1 in a Nutshell

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

Page 7: Spring 3.1 in a Nutshell

Environment and Profiles

7

Page 8: Spring 3.1 in a Nutshell

8

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 9: Spring 3.1 in a Nutshell

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

Page 10: Spring 3.1 in a Nutshell

10

Managing Property Sources

>  Standalone code

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

Page 11: Spring 3.1 in a Nutshell

11

Accessing Properties

>  By injecting the Environment

Page 12: Spring 3.1 in a Nutshell

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

Page 13: Spring 3.1 in a Nutshell

13

Configuring Profiles in XML

>  All bean definitions

>  Subset of bean definitions

Page 14: Spring 3.1 in a Nutshell

14

Configuring Profiles in Java Config

>  @Profile can also be used on components –  @Component, @Service, @Repository, etc.

Page 15: Spring 3.1 in a Nutshell

15

Activating Profiles (1/2)

>  Standalone code

>  In Web applications

Page 16: Spring 3.1 in a Nutshell

16

Activating Profiles (2/2)

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

>  With the Spring TestContext Framework

Page 17: Spring 3.1 in a Nutshell

Java-based Configuration

17

Page 18: Spring 3.1 in a Nutshell

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

Page 19: Spring 3.1 in a Nutshell

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

Page 20: Spring 3.1 in a Nutshell

20

Hibernate and JPA

>  Hibernate SessionFactory builder APIs –  SessionFactoryBuilder –  AnnotationSessionFactoryBuilder

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

Page 21: Spring 3.1 in a Nutshell

21

Java Configuration Example

Page 22: Spring 3.1 in a Nutshell

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

Page 23: Spring 3.1 in a Nutshell

23

@Configuration Testing Example

Page 24: Spring 3.1 in a Nutshell

Caching

24

Page 25: Spring 3.1 in a Nutshell

25

Caching Abstraction

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

>  Key annotations @Cacheable and @CacheEvict

Page 26: Spring 3.1 in a Nutshell

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

Page 27: Spring 3.1 in a Nutshell

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.

Page 28: Spring 3.1 in a Nutshell

28

Cache Configuration

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

Page 29: Spring 3.1 in a Nutshell

MVC and REST

29

Page 30: Spring 3.1 in a Nutshell

30

How We Configure Spring MVC Today

>  Built-in defaults –  Based on DispatcherServlet.properties

>  Spring MVC namespace

–  <mvc:annotation:driven>, <mvc:interceptors>, …

Page 31: Spring 3.1 in a Nutshell

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?

Page 32: Spring 3.1 in a Nutshell

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

Page 33: Spring 3.1 in a Nutshell

33

A More Complete Example …

>  Add component scanning for @Controllers and other beans

Page 34: Spring 3.1 in a Nutshell

34

Q: Where Is The “Enabled” Configuration ?!

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

Page 35: Spring 3.1 in a Nutshell

35

How Do I Customize All This?

>  Simply implement the WebMvcConfigurer interface Allows selective overriding

Page 36: Spring 3.1 in a Nutshell

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

Page 37: Spring 3.1 in a Nutshell

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

Page 38: Spring 3.1 in a Nutshell

38

Path Variables in The Model

>  @PathVariable arguments automatically added to the model

These can be deleted

Page 39: Spring 3.1 in a Nutshell

39

URI Templates in Redirect Strings

>  URL templates supported in “redirect:” strings

Expanded from model attributes, which now include @PathVariables

Page 40: Spring 3.1 in a Nutshell

40

URI Template Variables in Data Binding

>  URI template variables used in data binding

Page 41: Spring 3.1 in a Nutshell

41

Matching MediaTypes before Spring MVC 3.1

>  Using the 'headers' condition

Page 42: Spring 3.1 in a Nutshell

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)

Page 43: Spring 3.1 in a Nutshell

Servlet 3.0

43

Page 44: Spring 3.1 in a Nutshell

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

Page 45: Spring 3.1 in a Nutshell

45

WebApplicationInitializer Example

Page 46: Spring 3.1 in a Nutshell

Odds & Ends

46

Page 47: Spring 3.1 in a Nutshell

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

Page 48: Spring 3.1 in a Nutshell

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

Page 49: Spring 3.1 in a Nutshell

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

Page 50: Spring 3.1 in a Nutshell

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/

Page 51: Spring 3.1 in a Nutshell

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

Page 52: Spring 3.1 in a Nutshell

Sam Brannen [email protected] 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

Q&A