1. Improve null safety
  2. Replace lambda with method reference

Comment From: wilkinsona

Thanks, @quaff.

I've merged this without the null checks. We can consider this topic again as and when we tackle https://github.com/spring-projects/spring-boot/issues/10712, building on the Framework team's experience where they treat main and test source differently.