springboot3-upgrade
Upgrading Springboot2 to Springboot3 - notes
Important:: Bckup your project!
Update Java version to 17 in
pom.xml1
2
3<properties>
<java.version>17</java.version>
</properties>Upgrade Spring Boot version to 3.x.x in
pom.xml1
2
3
4
5
6<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version> <!-- Replace with the latest Spring Boot 3.x version -->
<relativePath/> <!-- lookup parent from repository -->
</parent>Update dependencies in
pom.xmlAs Spring Boot manages many dependencies, we need to go though each dependency we overrode for Spring Boot with the correct version. Usually the dependencies are:
spring-boot-starter-web,spring-boot-starter-security, etc.Handle namespace changes
The major changes in Spring Boot 3 is the migration from
java.*tojakarta.*Update Spring Security configuration
Replace
WebSecurityConfigurerAdapterBefore (Spring Boot 2):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt();
}
}After (Spring Boot 3 with Spring Security 6):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(authz -> authz
.anyRequest().authenticated()
)
.oauth2ResourceServer(oauth2 -> oauth2
.jwt()
);
return http.build();
}
}Also the test cases would be impacted, which are supposed to be updated as well.
Before:
1
2
3
4
5@RunWith(SpringRunner.class)
@WebMvcTest(MyController.class)
public class MyControllerTest {
// ...
}After:
1
2
3
4
5
6
7
8import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@WebMvcTest(MyController.class)
public class MyControllerTest {
// ...
}In Spring Boot 3, the framework introduces Ahead-of-Time (AOT) compilation as part of its support for GraalVM Native Image generation. However, if you don’t need AOT compilation for your project or are facing issues related to it, you can easily disable it.
Disable in
propertyfile1
spring.aot.enabled=falseDisable
AOTinpom.xml1
2
3
4
5
6
7
8
9
10
11
12<build>
<plugins>
<!-- Spring Boot Maven Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<aot>false</aot> <!-- Disable AOT -->
</configuration>
</plugin>
</plugins>
</build>Disable
AOTfor testing inpom.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>test</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<aot>false</aot>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>@DisabledInAotModefor the test cases with@MockBean