@@ -365,6 +365,18 @@ public Object getObject() throws BeansException {
365
365
}
366
366
367
367
// Check if required type matches the type of the actual bean instance.
368
+ // Note that the following return declarations are technically violating the
369
+ // non-null policy for the getBean methods: However, these will only result
370
+ // in null under very specific circumstances: such as a user-declared factory
371
+ // method returning null or a user-provided FactoryBean.getObject() returning
372
+ // null, without any custom post-processing of such null values. We will pass
373
+ // them on as null to corresponding injection points in that exceptional case
374
+ // but do not expect user-level getBean callers to deal with such null values.
375
+ // In the end, regular getBean callers should be able to assign the outcome
376
+ // to non-null variables/arguments without being compromised by rather esoteric
377
+ // corner cases, in particular in functional configuration and Kotlin scenarios.
378
+ // A future Spring generation might eventually forbid null values completely
379
+ // and throw IllegalStateExceptions instead of leniently passing them through.
368
380
if (requiredType != null && bean != null && !requiredType .isInstance (bean )) {
369
381
try {
370
382
return getTypeConverter ().convertIfNecessary (bean , requiredType );
@@ -377,6 +389,7 @@ public Object getObject() throws BeansException {
377
389
throw new BeanNotOfRequiredTypeException (name , requiredType , bean .getClass ());
378
390
}
379
391
}
392
+ // For the nullability warning, see the elaboration in the comment above.
380
393
return (T ) bean ;
381
394
}
382
395
0 commit comments