-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`beanFactory.isTypeMatch` may lead to the initialization of FactoryBean, dubbo consumer is a ReferenceBean which implements FactoryBean. support spring 3 related to #2328 #3865 #4161 #4164
- Loading branch information
Showing
1 changed file
with
15 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,8 @@ | |
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
|
@@ -29,6 +29,16 @@ | |
* @author Jason Song([email protected]) | ||
*/ | ||
public class BeanRegistrationUtil { | ||
// reserved bean definitions, we should consider drop this if we will upgrade Spring version | ||
private static final Map<String, String> RESERVED_BEAN_DEFINITIONS = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
RESERVED_BEAN_DEFINITIONS.put( | ||
"org.springframework.context.support.PropertySourcesPlaceholderConfigurer", | ||
"org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer" | ||
); | ||
} | ||
|
||
public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry registry, String beanName, | ||
Class<?> beanClass) { | ||
return registerBeanDefinitionIfNotExists(registry, beanName, beanClass, null); | ||
|
@@ -42,15 +52,16 @@ public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry r | |
|
||
String[] candidates = registry.getBeanDefinitionNames(); | ||
|
||
String reservedBeanDefinition = RESERVED_BEAN_DEFINITIONS.get(beanClass.getName()); | ||
for (String candidate : candidates) { | ||
BeanDefinition beanDefinition = registry.getBeanDefinition(candidate); | ||
if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) { | ||
return false; | ||
} | ||
|
||
if (beanDefinition instanceof AnnotatedBeanDefinition) { | ||
MethodMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getFactoryMethodMetadata(); | ||
if (metadata != null && Objects.equals(metadata.getReturnTypeName(), beanClass.getName())) { | ||
if (reservedBeanDefinition != null && beanDefinition.getSource() != null && beanDefinition.getSource() instanceof MethodMetadata) { | ||
MethodMetadata metadata = (MethodMetadata) beanDefinition.getSource(); | ||
if (Objects.equals(reservedBeanDefinition, String.format("%s#%s", metadata.getDeclaringClassName(), metadata.getMethodName()))) { | ||
return false; | ||
} | ||
} | ||
|