2727import java .util .Map ;
2828import java .util .Set ;
2929
30+ import org .jspecify .annotations .Nullable ;
31+
3032import org .springframework .beans .factory .ListableBeanFactory ;
3133import org .springframework .boot .Banner .Mode ;
3234import org .springframework .boot .SpringApplication ;
@@ -118,19 +120,24 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
118120 apply (context , event .getSpringApplication (), environment );
119121 }
120122
121- private ConfigurableApplicationContext findBootstrapContext (ParentContextApplicationContextInitializer initializer ,
122- String configName ) {
123+ private @ Nullable ConfigurableApplicationContext findBootstrapContext (
124+ ParentContextApplicationContextInitializer initializer , String configName ) {
125+ ConfigurableApplicationContext parent = null ;
123126 Field field = ReflectionUtils .findField (ParentContextApplicationContextInitializer .class , "parent" );
124- ReflectionUtils .makeAccessible (field );
125- ConfigurableApplicationContext parent = safeCast (ConfigurableApplicationContext .class ,
126- ReflectionUtils .getField (field , initializer ));
127- if (parent != null && !configName .equals (parent .getId ())) {
128- parent = safeCast (ConfigurableApplicationContext .class , parent .getParent ());
127+ if (field != null ) {
128+ ReflectionUtils .makeAccessible (field );
129+ Object value = ReflectionUtils .getField (field , initializer );
130+ if (value != null ) {
131+ parent = safeCast (ConfigurableApplicationContext .class , value );
132+ }
133+ if (parent != null && !configName .equals (parent .getId ()) && parent .getParent () != null ) {
134+ parent = safeCast (ConfigurableApplicationContext .class , parent .getParent ());
135+ }
129136 }
130137 return parent ;
131138 }
132139
133- private <T > T safeCast (Class <T > type , Object object ) {
140+ private <T > @ Nullable T safeCast (Class <T > type , Object object ) {
134141 try {
135142 return type .cast (object );
136143 }
@@ -177,7 +184,7 @@ private ConfigurableApplicationContext bootstrapServiceContext(ConfigurableEnvir
177184 .logStartupInfo (false )
178185 .web (WebApplicationType .NONE );
179186 final SpringApplication builderApplication = builder .application ();
180- if (builderApplication .getMainApplicationClass () == null ) {
187+ if (builderApplication .getMainApplicationClass () == null && application . getMainApplicationClass () != null ) {
181188 // gh_425:
182189 // SpringApplication cannot deduce the MainApplicationClass here
183190 // if it is booted from SpringBootServletInitializer due to the
@@ -225,7 +232,7 @@ private void mergeDefaultProperties(MutablePropertySources environment, MutableP
225232 String name = DEFAULT_PROPERTIES ;
226233 if (bootstrap .contains (name )) {
227234 PropertySource <?> source = bootstrap .get (name );
228- if (!environment .contains (name )) {
235+ if (!environment .contains (name ) && source != null ) {
229236 environment .addLast (source );
230237 }
231238 else {
@@ -246,6 +253,9 @@ private void mergeDefaultProperties(MutablePropertySources environment, MutableP
246253
247254 private void mergeAdditionalPropertySources (MutablePropertySources environment , MutablePropertySources bootstrap ) {
248255 PropertySource <?> defaultProperties = environment .get (DEFAULT_PROPERTIES );
256+ if (defaultProperties == null ) {
257+ defaultProperties = new MapPropertySource (DEFAULT_PROPERTIES , new LinkedHashMap <>());
258+ }
249259 ExtendedDefaultPropertySource result = defaultProperties instanceof ExtendedDefaultPropertySource
250260 ? (ExtendedDefaultPropertySource ) defaultProperties
251261 : new ExtendedDefaultPropertySource (DEFAULT_PROPERTIES , defaultProperties );
@@ -456,7 +466,7 @@ public void add(PropertySource<?> source) {
456466 }
457467
458468 @ Override
459- public Object getProperty (String name ) {
469+ public @ Nullable Object getProperty (String name ) {
460470 if (this .sources .containsProperty (name )) {
461471 return this .sources .getProperty (name );
462472 }
@@ -480,7 +490,7 @@ public String[] getPropertyNames() {
480490 }
481491
482492 @ Override
483- public Origin getOrigin (String name ) {
493+ public @ Nullable Origin getOrigin (String name ) {
484494 return this .sources .getOrigin (name );
485495 }
486496
0 commit comments