4. Spring 4.1增强和新功能

4.1 JMS改进

Spring 4.1引入了一个更简单的基础架构,使用 @JmsListener注解bean方法来注册JMS监听端点。XML命名空间已经通过增强来支持这种新的方式(jms:annotation-driven),它也可以完全通过Java配置( @EnableJms, JmsListenerContainerFactory)来配置架构。也可以使用 JmsListenerConfigurer注解来注册监听端点。

Spring 4.1还调整了JMS的支持,使得你可以从spring-messaging在Spring4.0引入的抽象获益,即:

  • 消息监听端点可以有更为灵活的签名,并且可以从标准的消息注解获益,例如@Payload@Header@Headers@SendTo注解。另外,也可以使用一个标准的消息,以代替javax.jms.Message作为方法参数。
  • 一个新的可用 JmsMessageOperations接口和允许操作使用Message抽象的JmsTemplate

最后,Spring 4.1提供了其他各种各样的改进:

  • JmsTemplate中的同步请求-答复操作支持
  • 监听器的优先权可以指定每个<jms:listener/>元素
  • 消息侦听器容器恢复选项可以通过使用 BackOff 实现进行配置
  • JMS 2.0消费者支持共享

4.2 Caching(缓存)改进

Spring 4.1 支持JCache (JSR-107)注解使用Spring的现有缓存配置和基础结构的抽象;使用标准注解不需要任何更改。

Spring 4.1也大大提高了自己的缓存抽象:

  • 缓存可以在运行时使用CacheResolver解决。因此使用value参数定义的缓存名称不在是强制性的。
  • 更多的操作级自定义项:缓存解析器,缓存管理器,键值生成器
  • 一个新的@CacheConfig类级别注解允许在类级别上共享常用配置,不需要启用任何缓存操作。
  • 使用CacheErrorHandler更好的处理缓存方法的异常

Spring 4.1为了在CacheInterface添加一个新的putIfAbsent方法也做了重大的更改。

4.3 Web改进

  • The existing support for resource handling based on the ResourceHttpRequestHandler has been expanded with new abstractions ResourceResolver, ResourceTransformer, and ResourceUrlProvider. 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 16.16.7, “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 to DeferredResult where an underlying service (or perhaps a call to AsyncRestTemplate) already returns ListenableFuture.
  • @ModelAttribute methods are now invoked in an order that respects inter-dependencies. See SPR-6299.
  • Jackson’s @JsonView is supported directly on @ResponseBody and ResponseEntity 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 序列化视图” for details.
  • JSONP is now supported with Jackson. See the section called “支持 Jackson JSONP”.
  • A new lifecycle option is available for intercepting @ResponseBody and ResponseEntity methods just after the controller method returns and before the response is written. To take advantage declare an @ControllerAdvice bean that implements ResponseBodyAdvice. The built-in support for @JsonView and JSONP take advantage of this. See Section 16.4.1, “使用 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 if jackson-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 example FooController with method handleFoo is named "FC#handleFoo". The naming strategy is pluggable. It is also possible to name an @RequestMapping explicitly through its name attribute. A new mvcUrl function in the Spring JSP tag library makes this easy to use in JSP pages. See Section 16.7.2, “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 16.16.6, “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 16.16.9, “Path Matching”.
  • Groovy markup template support (based on Groovy 2.3). See the GroovyMarkupConfigurer and respecitve ViewResolver and ‘View’ implementations.

4.4 WebSocket STOMP消息改进

  • SockJS (Java) client-side support. See SockJsClient and classes in same package.
  • New application context events SessionSubscribeEvent and SessionUnubscribeEvent published when STOMP clients subscribe and unsubscribe.
  • New "websocket" scope. See Section 20.4.13, “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 20.4.15, “Runtime 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.
  • STOMP/WebSocket connections that have not activity 60 seconds after the WebSocket session is established. See SPR-11884.

4.5 测试改进