The Spring ecosystem is constantly evolving to provide developers with the best tools for building modern, robust applications. With the upcoming release of Spring Framework 7.0, the community is eagerly anticipating the next major iteration of Spring Boot: version 4.0.

Spring Boot 4.0 is a major release of the Spring Boot framework, introducing significant enhancements for developers.

Key features and improvements in Spring Boot 4 includes:

Core Enhancements

Spring Boot 4.0 is built upon Spring Framework 7.0 which is major upcoming release of Spring Framework (projected for General Availability in November 2025).

  1. Java 17+ Baseline

    • Spring Boot 4 will require Java 17 or higher while at the same time recommending JDK 25 as the latest LTS release.
    • This baseline enables Spring Boot 4 to leverage the modern features of Java like pattern matching, sealed classes/interfaces, records, virtual threads and includes JVM level performance improvements.
  2. Jakarta EE 11+

    Spring Boot 4 supports Jakarta EE 11 and later, which means you have to move away from older javax.* and use jakarta.* instead.

  3. Enhanced Native Compilation with AOT

    Spring Boot 4 further refines native image generation using GraalVM and Ahead-of-Time (AOT) processing.

  4. Kotlin 2.2+ Support

    Spring Boot 4 introduces support for Kotlin 2.2+, enabling developers to leverage the latest features and improvements in the Kotlin language.

  5. Micrometer 2.0+ Support

    Spring Boot 4 is bundled with Micrometer 2.0+, offering better metrics, and improved observability support.

  6. Spring Security 7 Integration

    Includes enhanced security with OAuth 2.1 and OpenID Connect protection.

  7. Improved Configuration Properties

    Includes smarter record-based configuration with native hints for GraalVM and built-in validation.

    @ConfigurationProperties("app.database")
    public record DatabaseProperties(String url, String username, String password, int poolSize) {}
    
  8. Null-Safety Improvements

    The framework adopts JSpecify annotations to declare the null-safety of its API.

Other notable improvements

API Version Control

The new update introduces streamlined support for API Versioning through annotations. This simplifies API evolution and maintenance.

Example:

@RestController()
@RequestMapping("/greet")
class DemoController {
    @GetMapping("", version = "1")
    fun greetV1(): Map<String, String> = mapOf("message" to "Hello! from greetV1()")

    @GetMapping("", version = "2")
    fun greetV2(): Map<String, String> = mapOf("message" to "Hello! from greetV2()")
}

To make above code work, you need to provide the ApiVersionStrategy. If you run above code without providing ApiVersionStrategy, app will throw:

Caused by: java.lang.IllegalStateException: API version specified, but no ApiVersionStrategy configured

Providing ApiVersionStrategy:

@Configuration
class WebConfiguration : WebMvcConfigurer {
	override fun configureApiVersioning(configurer: ApiVersionConfigurer) {
		configurer.useRequestHeader("X-API-Version")
	}
}

When calling api you have to provide the request header X-API-Version

curl -H "X-API-Version: 2"  http://0.0.0.0:8080/greet

# Response: {"message":"Hello! from greetV2()"}

Programmatic Bean Registration (BeanRegistrar)

Spring Framework 7.0 introduces a new mechanism to register the bean programmatically using BeanRegistrar interface. This provides a more flexible approach to register a bean.

There are various benefits of providing the bean programmatically, for example, registering a bean based on some runtime conditions like active profiles.

Example:

class Foo()
data class Bar(val value: String)

// you can also use BeanRegistrarDsl in Kotlin
class FooBarBeanRegistrar : BeanRegistrar {
    override fun register(
        registry: BeanRegistry,
        env: Environment
    ) {
        registry.registerBean("foo", Foo::class.java)

        // Register a bean with a custom supplier, potentially conditionally
        if (env.matchesProfiles("dev")) {
            registry.registerBean(Bar::class.java) { spec ->
                spec.supplier { Bar("Bar_001") }
                    .prototype()
                    .lazyInit()
                    .description("Bar 001 description")
            }
        }
    }
}

Importing FooBarBeanRegistrar

@Configuration
@Import(FooBarBeanRegistrar::class)
class AppConfig(
    private val foo: Foo,
    private val bar: Bar,
) {
    private val logger: Logger = LoggerFactory.getLogger(AppConfig::class.java)

    @Bean
    fun provideCommandLine(): CommandLineRunner = CommandLineRunner { args ->
        logger.info("foo = $foo")
        logger.info("bar = $bar")
    }
}

Declarative HTTP interface

Spring Boot 4 introduces enhanced support for declarative HTTP clients, building upon the existing RestClient and WebClient options.

It allows defining HTTP client interfaces using annotations, similar to Feign client. Spring generates the implementation at runtime, reducing boilerplate code for making HTTP requests.

Example:

@HttpExchange("https://jsonplaceholder.typicode.com")
interface TodoClient {
    @GetExchange("/todos")
    fun getTodos(): List<Todo>

    @GetExchange("/todos/{id}")
    fun getTodoById(@PathVariable id: Long): Todo?

    @PostExchange("/todos")
    fun createTodo(@RequestBody todo: Map<String, String>)
}

@Configuration
@ImportHttpServices(TodoClient::class)
class ClientsConfiguration

Conclusion

Spring Boot 4 aims to be a transformative release, introducing numerous enhancements to the Spring Framework that significantly simplify application development.