New Features in Spring 3.1

This post will feature some of the major features and enhancements of Spring 3.1. Released in the year 2011, Spring 3.1 introduced many new exciting features as listed and described in this article. This article would provide very good idea on the new features introduced and how it is different from the previous versions. If you are developer who are interested in choosing the spring version, this article would be very useful.

also read: follow us on @twitter and @facebook

Cache Abstraction

Spring 3.1 has introduced a new feature called Cache Abstraction. Through this feature Spring can add caching concept to any existing application with very few changes in the code. Some of the features of Cache abstraction are:

  • Declarative caching for Spring applications, which means minimal impact on code and we can plug in various caching solutions to the existing applications.
  • This feature is not an implementation, but an abstraction.
  • This feature frees you from writing logic, but does not provide stores. Cache abstraction provides integration with two storage’s – one on top of the JDK ConcurrentMap and one for ehcache library.
  • Key annotations are @Cacheable and @CacheEvict.
  • You can use Spring EL for key and condition attributes
  • Annotations triggered by <cache:annotation-driven/>.

Environment Abstraction

Injectable environment abstraction API, org.springframework.core.env.Environment, has been integrated throughout the spring container. This has two core concepts:

  • Bean Profile
  • Property Source

These explained in the next sections. Spring’s Environment abstraction provides a single location to configure both profiles and properties.

Bean Definition Profiles

Bean definition profiles represent a general-purpose way that allows for registration of different beans in different environments. i.e it determines which bean definitions should be registered for a given deployment context. Some of the features of Bean Definition Profiles are:

  • As mentioned above , this feature helps logical grouping of bean definitions, for e.g., dev, staging, prod
  • Configuration of XML is done via . Java-based configuration is done via @Profile.
  • Activation of profiles can be done programmatically, using servlet init-params in web.xml, through system property, in tests via @ActiveProfiles.

PropertySource Abstraction

As mentioned above, Spring’s Environment abstraction provides search operations over a configurable hierarchy of property sources.
A PropertySource is a simple abstraction over any source of key-value pairs. E.g:property files, system properties,
servlet context, JNDI, etc.

Spring’s DefaultEnvironment is configured with two PropertySource objects

  1. one representing the set of JVM system properties (System.getProperties())
  2. one representing the set of system environment variables (System.getenv())

Code equivalents for Spring’s XML namespaces

Spring XML namespace elements like <context:component-scan/>, <tx:annotation-driven/> and <mvc:annotation-driven>, have code-based equivalents since Spring 3.1 most in the form of @Enable annotations. These work in conjunction with the Spring’s @Configuration classes.

@Enable Annotations:

  • Applied at the class-level on @Configuration classes
  • Roughly equivalent to their XML namespace counterparts
  • Annotations available in Spring 3.1 are: @EnableTransactionManagement, @EnableAsync, @EnableScheduling, @EnableLoadTimeWeaving, @EnableWebMvc, @EnableCaching, @EnableAspectJAutoProxy, @EnableLoadTimeWeaving, @EnableSpringConfigured

Support for Hibernate 4.x

Spring 3.1, support Hibernate 4.x, natively and through JPA. Native support is through a dedicated org.springframework.orm.hibernate4 package, dealing with package rearrangements in the Hibernate API. It also presers the compatibility with Hibernate 3.2.

TestContext framework support for @Configuration classes and bean definition profiles

@Configuration (JavaConfig) annotation has been added in Spring 3.0. TestContext framework (located in the org.springframework.test.context package) provides annotation-driven testing support. Spring 3.1 adds loader attribute to @ContextConfiguration for @Configuration.

c: namespace for more concise constructor injection

c: Namespace for XML Configuration, is a shortcut for <constructor-arg>. It allows usage of inline argument values. It is analogous to existing “p:” namespace.

Support for injection against non-standard JavaBeans setters

Spring 3.1 now supports injection against non-standard JavaBeans setters, useful with method-chaining, where setter methods return a reference to ‘this’. In earlier Spring versions, for Spring to inject against a property method, the method had to conform strictly to JavaBeans property rules, namely that the setter method must return void.

Support for Servlet 3 code-based configuration of Servlet Container

Spring 3.1 supports Servlet 3. It provides :

  • Explicit support for Servlet 3.0 containers like Tomcat 7 and GlassFish 3 while preserving compatibility with Servlet 2.4+.
  • Support for XML-free web application setup (no web.xml) i.e Servlet 3.0′s ServletContainerInitializer plus Spring 3.1′s AnnotationConfigWebApplicationContext plus the environment abstraction. This means that there’s no need for web.xml to boodstrap the Spring 3.1 application.
  • Exposure of native Servlet 3.0 functionality in Spring MVC, i.e support for asynchronous request processing and standard Servlet 3.0 file upload support behind Spring’s MultipartResolver abstraction.

JPA EntityManagerFactory bootstrapping without persistence.xml

With Spring 3.1, the JPA EntityManagerFactory can be bootstrapped without persistence.xml or other metadata files. Using the LocalContainerEntityManagerFactoryBean packagesToScan property, you can specify the base packages to scan for @Entity classes. This is similar to Spring’s component-scan feature for regular Spring beans.

New HandlerMethod-based Support Classes For Annotated Controller Processing

HandlerMethod is a proper abstraction for the selected “handler” in Spring MVC. HandlerMethod support classes are:

  • RequestMappingHandlerMapping
  • RequestMappingHandlerAdapter
  • ExceptionHandlerExceptionResolver

“consumes” and “produces” conditions in @RequestMapping

Since Spring 3.1 we can specify mediatype that is accepted and provided by mapped controller method. It is much more powerful than specifying Content-Type or Accept headers as used to do before.

@RequestMapping(method = POST, consumes ="application/json", produces="application/json")

Flash Attributes and RedirectAttributes

Flash attributes provide a way for one request to store attributes intended for use in another, most commonly used when using redirects like the Post/Redirect/Get pattern. Flash attributes are stored in FlashMap and stored in HTTP sessions, making it available to the request after the redirect and then removed immediately.

URI Template Variable Enhancements

URI template variables are now used in addition to request parameters when binding a request to @ModelAttribute arguments. @PathVariable method argument values are also merged into the model before rendering. A redirect string can contain placeholders for URI variables, and a @ModelAttribute method argument can be instantiated from a URI template variable as long as there is a registered Converter or PropertyEditor.

@Valid On @RequestBody Controller Method Arguments

Request body can be validated using Bean Validation API. An @RequestBody method argument can be annotated with @Valid to invoke automatic validation as seen in the example below:

@RequestMapping(method = POST, consumes = "application/json")
public Task create(@Valid @RequestBody Task task) {
// No BindingResult in method signature

@RequestPart Annotation On Controller Method Arguments

The @RequestPart annotation can be used to associate a multipart/form-data request part with a method argument. It allows you to have the content of a specific multipart passed through an HttpMessageConverter, taking into consideration the multipart content-type.

UriComponentsBuilder and UriComponents

  • UriComponents : class is an immutable container of URI components providing access to all contained URI components.
  • UriComponentsBuilder: class help create UriComponents instances.

Together the above two classes give fine-grained control over all aspects of preparing a URI including construction, expansion from URI template variables, and encoding.


In this post I highlighted all the new features of Spring 3.1 though not in details. For further details you can refer the official documentation of Spring. If you are interested in receiving the future articles, please subscribe here. follow us on @twitter and @facebook.



About Manisha Patil

Manisha S Patil, currently residing at Pune India. She is currently working as freelance writer for websites. She had earlier worked at Caritor Bangalore, TCS Bangalore and Sungard Pune. She has 5 years of experience in Java/J2EE technologies.

Speak Your Mind