1919import java .util .Map ;
2020import java .util .Objects ;
2121
22- import org .springframework .beans .factory .BeanFactory ;
22+ import org .springframework .beans .factory .annotation . AnnotatedBeanDefinition ;
2323import org .springframework .beans .factory .config .BeanDefinition ;
2424import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
2525import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
26+ import org .springframework .core .type .MethodMetadata ;
2627
2728/**
2829 * @author Jason Song(song_s@ctrip.com)
@@ -41,17 +42,15 @@ public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry r
4142
4243 String [] candidates = registry .getBeanDefinitionNames ();
4344
44- if (registry instanceof BeanFactory ) {
45- final BeanFactory beanFactory = (BeanFactory ) registry ;
46- for (String candidate : candidates ) {
47- if (beanFactory .isTypeMatch (candidate , beanClass )) {
48- return false ;
49- }
45+ for (String candidate : candidates ) {
46+ BeanDefinition beanDefinition = registry .getBeanDefinition (candidate );
47+ if (Objects .equals (beanDefinition .getBeanClassName (), beanClass .getName ())) {
48+ return false ;
5049 }
51- } else {
52- for ( String candidate : candidates ) {
53- BeanDefinition beanDefinition = registry . getBeanDefinition ( candidate );
54- if (Objects .equals (beanDefinition . getBeanClassName (), beanClass .getName ())) {
50+
51+ if ( beanDefinition instanceof AnnotatedBeanDefinition ) {
52+ MethodMetadata metadata = (( AnnotatedBeanDefinition ) beanDefinition ). getFactoryMethodMetadata ( );
53+ if (metadata != null && Objects .equals (metadata . getReturnTypeName (), beanClass .getName ())) {
5554 return false ;
5655 }
5756 }
0 commit comments