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