On this page
4. New Features and Enhancements in Spring Framework 4.1
Version 4.1 included a number of improvements, as described in the following sections:
Spring 4.1 introduces a much simpler infrastructure to register JMS listener endpoints by annotating bean methods with @JmsListener
. The XML namespace has been enhanced to support this new style (jms:annotation-driven
), and it is also possible to fully configure the infrastructure using Java config (@EnableJms
, JmsListenerContainerFactory
). It is also possible to register listener endpoints programmatically using JmsListenerConfigurer
.
Spring 4.1 also aligns its JMS support to allow you to benefit from the spring-messaging
abstraction introduced in 4.0, that is:
- Message listener endpoints can have a more flexible signature and benefit from standard messaging annotations such as
@Payload
,@Header
,@Headers
, and@SendTo
. It is also possible to use a standardMessage
in lieu ofjavax.jms.Message
as method argument. - A new
JmsMessageOperations
interface is available and permitsJmsTemplate
like operations using theMessage
abstraction.
Finally, Spring 4.1 provides additional miscellaneous improvements:
- Synchronous request-reply operations support in
JmsTemplate
- Listener priority can be specified per
<jms:listener/>
element - Recovery options for the message listener container are configurable using a
BackOff
implementation - JMS 2.0 shared consumers are supported
Spring 4.1 supports JCache (JSR-107) annotations using Spring’s existing cache configuration and infrastructure abstraction; no changes are required to use the standard annotations.
Spring 4.1 also improves its own caching abstraction significantly:
- Caches can be resolved at runtime using a
CacheResolver
. As a result thevalue
argument defining the cache name(s) to use is no longer mandatory. - More operation-level customizations: cache resolver, cache manager, key generator
- A new
@CacheConfig
class-level annotation allows common settings to be shared at the class level without enabling any cache operation. - Better exception handling of cached methods using
CacheErrorHandler
Spring 4.1 also has a breaking change in the Cache
interface as a new putIfAbsent
method has been added.
- The existing support for resource handling based on the
ResourceHttpRequestHandler
has been expanded with new abstractionsResourceResolver
,ResourceTransformer
, andResourceUrlProvider
. A number of built-in implementations provide support for versioned resource URLs (for effective HTTP caching), locating gzipped resources, generating an HTML 5 AppCache manifests, and more. See Section 22.16.9, “Serving of Resources”. - JDK 1.8’s
java.util.Optional
is now supported for@RequestParam
,@RequestHeader
, and@MatrixVariable
controller method arguments. ListenableFuture
is supported as a return value alternative toDeferredResult
where an underlying service (or perhaps a call toAsyncRestTemplate
) already returnsListenableFuture
.@ModelAttribute
methods are now invoked in an order that respects inter-dependencies. See SPR-6299 .- Jackson’s
@JsonView
is supported directly on@ResponseBody
andResponseEntity
controller methods for serializing different amounts of detail for the same POJO (e.g. summary vs. detail page). This is also supported with View-based rendering by adding the serialization view type as a model attribute under a special key. See the section called “Jackson Serialization View Support” for details. - JSONP is now supported with Jackson. See the section called “Jackson JSONP Support”.
- A new lifecycle option is available for intercepting
@ResponseBody
andResponseEntity
methods just after the controller method returns and before the response is written. To take advantage declare an@ControllerAdvice
bean that implementsResponseBodyAdvice
. The built-in support for@JsonView
and JSONP take advantage of this. See Section 22.4.1, “Intercepting requests with a HandlerInterceptor”. There are three new
HttpMessageConverter
options:- Gson — lighter footprint than Jackson; has already been in use in Spring Android.
- Google Protocol Buffers — efficient and effective as an inter-service communication data protocol within an enterprise but can also be exposed as JSON and XML for browsers.
- Jackson based XML serialization is now supported through the jackson-dataformat-xml extension. When using
@EnableWebMvc
or<mvc:annotation-driven/>
, this is used by default instead of JAXB2 ifjackson-dataformat-xml
is in the classpath.
- Views such as JSPs can now build links to controllers by referring to controller mappings by name. A default name is assigned to every
@RequestMapping
. For exampleFooController
with methodhandleFoo
is named "FC#handleFoo". The naming strategy is pluggable. It is also possible to name an@RequestMapping
explicitly through its name attribute. A newmvcUrl
function in the Spring JSP tag library makes this easy to use in JSP pages. See Section 22.7.3, “Building URIs to Controllers and methods from views”. ResponseEntity
provides a builder-style API to guide controller methods towards the preparation of server-side responses, e.g.ResponseEntity.ok()
.RequestEntity
is a new type that provides a builder-style API to guide client-side REST code towards the preparation of HTTP requests.MVC Java config and XML namespace:
- View resolvers can now be configured including support for content negotiation, see Section 22.16.8, “View Resolvers”.
- View controllers now have built-in support for redirects and for setting the response status. An application can use this to configure redirect URLs, render 404 responses with a view, send "no content" responses, etc. Some use cases are listed here .
- Path matching customizations are frequently used and now built-in. See Section 22.16.11, “Path Matching”.
- Groovy markup template support (based on Groovy 2.3). See the
GroovyMarkupConfigurer
and respecitveViewResolver
and `View' implementations.
- SockJS (Java) client-side support. See
SockJsClient
and classes in same package. - New application context events
SessionSubscribeEvent
andSessionUnsubscribeEvent
published when STOMP clients subscribe and unsubscribe. - New "websocket" scope. See Section 26.4.16, “WebSocket Scope”.
@SendToUser
can target only a single session and does not require an authenticated user.@MessageMapping
methods can use dot "." instead of slash "/" as path separator. See SPR-11660 .- STOMP/WebSocket monitoring info collected and logged. See Section 26.4.18, “Monitoring”.
- Significantly optimized and improved logging that should remain very readable and compact even at DEBUG level.
- Optimized message creation including support for temporary message mutability and avoiding automatic message id and timestamp creation. See Javadoc of
MessageHeaderAccessor
. - Close STOMP/WebSocket connections that have no activity within 60 seconds after the WebSocket session is established. See SPR-11884 .
Groovy scripts can now be used to configure the
ApplicationContext
loaded for integration tests in the TestContext framework.- See the section called “Context configuration with Groovy scripts” for details.
Test-managed transactions can now be programmatically started and ended within transactional test methods via the new
TestTransaction
API.- See the section called “Programmatic transaction management” for details.
SQL script execution can now be configured declaratively via the new
@Sql
and@SqlConfig
annotations on a per-class or per-method basis.- See Section 15.5.8, “Executing SQL scripts” for details.
Test property sources which automatically override system and application property sources can be configured via the new
@TestPropertySource
annotation.Default
TestExecutionListener
s can now be automatically discovered.Custom
TestExecutionListener
s can now be automatically merged with the default listeners.- See the section called “Merging TestExecutionListeners” for details.
The documentation for transactional testing support in the TestContext framework has been improved with more thorough explanations and additional examples.
- See Section 15.5.7, “Transaction management” for details.
- Various improvements to
MockServletContext
,MockHttpServletRequest
, and other Servlet API mocks. AssertThrows
has been refactored to supportThrowable
instead ofException
.- In Spring MVC Test, JSON responses can be asserted with JSON Assert as an extra option to using JSONPath much like it has been possible to do for XML with XMLUnit.
MockMvcBuilder
recipes can now be created with the help ofMockMvcConfigurer
. This was added to make it easy to apply Spring Security setup but can be used to encapsulate common setup for any 3rd party framework or within a project.MockRestServiceServer
now supports theAsyncRestTemplate
for client-side testing.