3. New Features and Enhancements in Spring 3.1

Building on the support introduced in Spring 3.0, Spring 3.1 is currently under development, and at the time of this writing Spring 3.1 M2 has just been released.

3.1 Overview of new features

This is a list of new features for Spring 3.1. Most features do not yet have dedicated reference documentation but do have Javadoc. In these cases, fully-qualified classnames are given.

3.1.1 Cache Abstraction

3.1.2 Bean Definition Profiles

  • XML profiles (SpringSource Team Blog)

  • Introducing @Profile (SpringSource Team Blog)

  • See org.springframework.context.annotation.Configuration Javadoc

  • See org.springframework.context.annotation.Profile Javadoc

3.1.3 Environment Abstraction

3.1.4 PropertySource Abstraction

  • Unified Property Management (SpringSource Team Blog)

  • See org.springframework.core.env.Environment Javadoc

  • See org.springframework.core.env.PropertySource Javadoc

  • See org.springframework.context.annotation.PropertySource Javadoc

3.1.5 Code equivalents for Spring's XML namespaces

Code-based equivalents to popular Spring XML namespace elements such as <tx:annotation-driven/> and <mvc:annotation-driven> have been developed, in the form of @Enable annotations, for use in conjunction with Spring's @Configuration classes.

  • See org.springframework.scheduling.annotation.Configuration Javadoc

  • See org.springframework.scheduling.annotation.EnableAsync Javadoc

  • See org.springframework.scheduling.annotation.EnableScheduling Javadoc

    See org.springframework.scheduling.annotation.EnableTransactionManagement Javadoc

  • See org.springframework.scheduling.annotation.EnableWebMvc Javadoc

3.1.6 Builder-style APIs for code-based Hibernate configuration

SessionFactoryBuilder and AnnotationSessionFactoryBuilder classes have been designed for use within @Bean methods in @Configuration classes.

  • See org.springframework.orm.hibernate3.SessionFactoryBuilder Javadoc

  • See org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBuilder Javadoc

3.1.7 TestContext framework support for @Configuration classes and bean definition profiles

The @ContextConfiguration annotation now supports supplying @Configuration classes for configuring the Spring TestContext. In addition, a new @ActiveProfiles annotation has been introduced to support declarative configuration of active bean definition profiles in ApplicationContext integration tests.

  • See org.springframework.test.context.ContextConfiguration Javadoc

3.1.8 c: namespace for more concise constructor injection

3.1.9 Support for injection against non-standard JavaBeans setters

Prior to Spring 3.1, in order to inject against a property method it had to conform strictly to JavaBeans property signature rules, namely that any 'setter' method must be void-returning. It is now possible in Spring XML to specify setter methods that return any object type. This is useful when considering designing APIs for method-chaining, where setter methods return a reference to 'this'.

3.1.10 Support for Servlet 3 code-based configuration of Servlet Container

The new WebApplicationInitializer builds atop Servlet 3.0's ServletContainerInitializer support to provide a programmatic alternative to the traditional web.xml.

3.1.11 Support for Servlet 3 MultipartResolver

  • See org.springframework.web.multipart.support.StandardServletMultipartResolver Javadoc

3.1.12 JPA EntityManagerFactory bootstrapping without persistence.xml

In standard JPA, persistence units get defined through META-INF/persistence.xml files in specific jar files which will in turn get searched for @Entity classes. In many cases, persistence.xml does not contain more than a unit name and relies on defaults and/or external setup for all other concerns (such as the DataSource to use, etc). For that reason, Spring 3.1 provides an alternative: LocalContainerEntityManagerFactoryBean accepts a 'packagesToScan' property, specifying base packages to scan for @Entity classes. This is analogous to AnnotationSessionFactoryBean's property of the same name for native Hibernate setup, and also to Spring's component-scan feature for regular Spring beans. Effectively, this allows for XML-free JPA setup at the mere expense of specifying a base package for entity scanning: a particularly fine match for Spring applications which rely on component scanning for Spring beans as well, possibly even bootstrapped using a code-based Servlet 3.0 initializer.

3.1.13 New HandlerMethod-based Support Classes For Annotated Controller Processing

Spring 3.1 introduces a new set of support classes for processing requests with annotated controllers:

  • RequestMappingHandlerMapping
  • RequestMappingHandlerAdapter
  • ExceptionHandlerExceptionResolver

These classes are a replacement for the existing:

  • DefaultAnnotationHandlerMapping
  • AnnotationMethodHandlerAdapter
  • AnnotationMethodHandlerExceptionResolver

The new classes were developed in response to many requests to make annotation controller support classes more customizable and open for extension. Whereas previously you could configure a custom annotated controller method argument resolver, with the new support classes you can customize the processing for any supported method argument or return value type.

  • See org.springframework.web.method.support.HandlerMethodArgumentResolver Javadoc
  • See org.springframework.web.method.support.HandlerMethodReturnValueHandler Javadoc

A second notable difference is the introduction of a HandlerMethod abstraction to represent an @RequestMapping method. This abstraction is used throughout by the new support classes as the handler instance. For example a HandlerInterceptor can cast the handler from Object to HandlerMethod and get access to the target controller method, its annotations, etc.

The new classes are enabled by default by the MVC namespace and by Java-based configuration via @EnableWebMvc. The existing classes will continue to be available but use of the new classes is recommended going forward.

3.1.14 Consumes and Produces @RequestMapping Conditions

Improved support for specifying media types consumed by a method through the 'Content-Type' header as well as for producible types specified through the 'Accept' header. See Section, “Consumable Media Types” and Section, “Producible Media Types”

3.1.15 Working With URI Template Variables In Controller Methods

@PathVariable method arguments are now automatically added to the model. If you declare any @PathVariable arguments on a controller method you no longer need to add them to the model.

Redirect view strings can now be URI templates. For example a controller can return "redirect:/blog/{year}/{month}". The URI template will be expanded with variables from the model, which of course includes @PathVariable method arguments that are now automatically added to the model.

URI template variables are now included in data binding in addition to request parameters, which are typically used for populating a model.

3.1.16 Validation For @RequestBody Method Arguments

An @RequestBody method argument annotated with @Valid is now automatically validated with the same Validator instance used to validate an @ModelAttribute method argument. Both the MVC namespace and @EnableWebMvc automatically configure a JSR-303 Validator adapter provided a JSR-303 implementation is available on the classpath.