@@ -525,42 +525,73 @@ protected boolean isTypeMatch(String name, ResolvableType typeToMatch, boolean a
525
525
// Check manually registered singletons.
526
526
Object beanInstance = getSingleton (beanName , false );
527
527
if (beanInstance != null && beanInstance .getClass () != NullBean .class ) {
528
+
529
+ // Determine target for FactoryBean match if necessary.
528
530
if (beanInstance instanceof FactoryBean <?> factoryBean ) {
529
531
if (!isFactoryDereference ) {
530
532
Class <?> type = getTypeForFactoryBean (factoryBean );
531
- return (type != null && typeToMatch .isAssignableFrom (type ));
532
- }
533
- else {
534
- return typeToMatch .isInstance (beanInstance );
535
- }
536
- }
537
- else if (!isFactoryDereference ) {
538
- if (typeToMatch .isInstance (beanInstance )) {
539
- // Direct match for exposed instance?
540
- return true ;
541
- }
542
- else if (typeToMatch .hasGenerics () && containsBeanDefinition (beanName )) {
543
- // Generics potentially only match on the target class, not on the proxy...
544
- RootBeanDefinition mbd = getMergedLocalBeanDefinition (beanName );
545
- Class <?> targetType = mbd .getTargetType ();
546
- if (targetType != null && targetType != ClassUtils .getUserClass (beanInstance )) {
547
- // Check raw class match as well, making sure it's exposed on the proxy.
548
- Class <?> classToMatch = typeToMatch .resolve ();
549
- if (classToMatch != null && !classToMatch .isInstance (beanInstance )) {
533
+ if (type == null ) {
534
+ return false ;
535
+ }
536
+ if (typeToMatch .isAssignableFrom (type )) {
537
+ return true ;
538
+ }
539
+ else if (typeToMatch .hasGenerics () && containsBeanDefinition (beanName )) {
540
+ RootBeanDefinition mbd = getMergedLocalBeanDefinition (beanName );
541
+ ResolvableType targetType = mbd .targetType ;
542
+ if (targetType == null ) {
543
+ targetType = mbd .factoryMethodReturnType ;
544
+ }
545
+ if (targetType == null ) {
550
546
return false ;
551
547
}
552
- if (typeToMatch .isAssignableFrom (targetType )) {
553
- return true ;
548
+ Class <?> targetClass = targetType .resolve ();
549
+ if (targetClass != null && FactoryBean .class .isAssignableFrom (targetClass )) {
550
+ Class <?> classToMatch = typeToMatch .resolve ();
551
+ if (classToMatch != null && !FactoryBean .class .isAssignableFrom (classToMatch ) &&
552
+ !classToMatch .isAssignableFrom (targetType .toClass ())) {
553
+ return typeToMatch .isAssignableFrom (targetType .getGeneric ());
554
+ }
555
+ }
556
+ else {
557
+ return typeToMatch .isAssignableFrom (targetType );
554
558
}
555
559
}
556
- ResolvableType resolvableType = mbd .targetType ;
557
- if (resolvableType == null ) {
558
- resolvableType = mbd .factoryMethodReturnType ;
560
+ return false ;
561
+ }
562
+ }
563
+ else if (isFactoryDereference ) {
564
+ return false ;
565
+ }
566
+
567
+ // Actual matching against bean instance...
568
+ if (typeToMatch .isInstance (beanInstance )) {
569
+ // Direct match for exposed instance?
570
+ return true ;
571
+ }
572
+ else if (typeToMatch .hasGenerics () && containsBeanDefinition (beanName )) {
573
+ // Generics potentially only match on the target class, not on the proxy...
574
+ RootBeanDefinition mbd = getMergedLocalBeanDefinition (beanName );
575
+ Class <?> targetType = mbd .getTargetType ();
576
+ if (targetType != null && targetType != ClassUtils .getUserClass (beanInstance )) {
577
+ // Check raw class match as well, making sure it's exposed on the proxy.
578
+ Class <?> classToMatch = typeToMatch .resolve ();
579
+ if (classToMatch != null && !classToMatch .isInstance (beanInstance )) {
580
+ return false ;
559
581
}
560
- return (resolvableType != null && typeToMatch .isAssignableFrom (resolvableType ));
582
+ if (typeToMatch .isAssignableFrom (targetType )) {
583
+ return true ;
584
+ }
585
+ }
586
+ ResolvableType resolvableType = mbd .targetType ;
587
+ if (resolvableType == null ) {
588
+ resolvableType = mbd .factoryMethodReturnType ;
561
589
}
590
+ return (resolvableType != null && typeToMatch .isAssignableFrom (resolvableType ));
591
+ }
592
+ else {
593
+ return false ;
562
594
}
563
- return false ;
564
595
}
565
596
else if (containsSingleton (beanName ) && !containsBeanDefinition (beanName )) {
566
597
// null instance registered
0 commit comments