Forward port of issue #34201 to 3.1.x.

Comment From: wilkinsona

We should review e67efe2674988ac7aea38c192497e704ad351470 as part of merging forwards any changes made to address https://github.com/spring-projects/spring-boot/issues/34201#issuecomment-1437414026 and https://github.com/spring-projects/spring-boot/issues/34201#issuecomment-1437420097.

Comment From: wilkinsona

The changes have been reverted in f6e208c.