What Is New in Spring Framework 4.1?
Spring Framework 4.1 delivers a range of refinements and new capabilities across its core container, web stack, messaging, and testing support. This update focuses on developer productivity and modern Java development needs.
| Category | Key Changes |
|---|---|
| Core Container | JSR-310 Date-Time types, @Conditional enhancements, dependency injection for java.util.Optional, and general performance improvements. |
| Web | Support for Jackson's @JsonView, GSON integration, XML improvements with JAXB2, and enhanced @RequestPart and CORS support. |
| Messaging | STOMP over WebSocket enhancements, including a "send-to-user" feature and better connection management. |
| Testing | New org.springframework.mock.web package, AopTestUtils, and refinements to transaction and SQL script execution in tests. |
How is dependency injection improved?
The core container now supports JSR-310 date-time types like LocalDate and LocalDateTime for direct injection into Spring-managed beans. This is a natural fit for applications using Java 8's time API.
You can also inject java.util.Optional to express non-mandatory dependencies. The @Conditional annotation is more flexible, allowing conditions to be placed on overriding @Bean methods.
@Bean
@Conditional(MyCondition.class)
public MyService myService() {
return new MyService();
}
What are the web layer updates for JSON and XML?
Jackson's @JsonView is now fully supported for controlling serialization output on @ResponseBody and ResponseEntity methods. This gives you fine-grained control over what data gets sent to the client.
GSON is supported as an alternative to Jackson for JSON rendering. For XML, JAXB2 is preferred over the deprecated JAXB 1.x support, aligning with modern Java environments.
Example with @JsonView
@RestController
public class UserController {
@JsonView(User.Summary.class)
@GetMapping("/user")
public User getUser() {
return new User();
}
}
What's new for file uploads and CORS?
The @RequestPart annotation now accepts a javax.servlet.http.Part argument, alongside MultipartFile. This provides more flexibility when handling file uploads in a Servlet 3.0+ environment.
Cross-Origin Resource Sharing (CORS) support is enhanced with global configuration and more granular rules on @RequestMapping methods. In practice, this simplifies securing your REST APIs for web clients.
How is WebSocket messaging enhanced?
The STOMP over WebSocket support gains a "send-to-user" feature, allowing messages to be routed to a specific user's session. Connection lifecycle events are also more accessible for application logic.
These improvements make building real-time, collaborative features more straightforward within the Spring ecosystem, reducing the need for low-level session management code.
What testing utilities were added?
A new org.springframework.mock.web package provides a comprehensive set of mock objects for the Servlet 3.1 API. This matters because it standardizes and improves test reliability for web components.
AopTestUtils helps you access underlying target objects behind Spring proxies in tests. There are also improvements for executing SQL scripts during transactional tests, ensuring a clean state.
// Get the real target behind a proxy
MyService target = AopTestUtils.getTargetObject(proxiedService);
FAQ
Can I use Java 8's Optional for optional dependencies?
Yes. Spring 4.1 allows you to declare a dependency of type Optional<Foo>. If a matching bean of type Foo exists, it will be wrapped in the Optional; otherwise, Optional.empty() is injected.
Does @JsonView work with XML views?
No, the @JsonView support is specific to JSON serialization using the Jackson library. For XML, you would use other mechanisms like JAXB annotations.
What's the benefit of the new mock.web package?
It provides up-to-date mock implementations for Servlet 3.1 APIs like MockHttpServletRequest. This means your unit tests can more accurately reflect a modern Servlet container's behavior.
How do I enable global CORS configuration?
You can define a CorsConfiguration bean and combine it with AbstractHandlerMapping or use XML namespace support. This is cleaner than annotating every single controller method.
Is JAXB 1.x still supported?
While it might still work, JAXB 1.x is officially deprecated in Spring 4.1. You should migrate to JAXB 2.x, which is the standard binding included in Java 6 and later.