Skip to content

Commit

Permalink
Fix #4152
Browse files Browse the repository at this point in the history
`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
lonre authored and nobodyiam committed Dec 25, 2021
1 parent a62881d commit 7c6c535
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Apollo 2.0.0
* [Split helm chart into another repo](https://github.com/apolloconfig/apollo/pull/4125)
* [fix gray publish refresh item status](https://github.com/apolloconfig/apollo/pull/4128)
* [Support only show difference keys when compare namespace](https://github.com/apolloconfig/apollo/pull/4165)
* [Fix the issue that property placeholder doesn't work for dubbo reference beans](https://github.com/apolloconfig/apollo/pull/4175)

------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,27 @@

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.type.MethodMetadata;

/**
* @author Jason Song(song_s@ctrip.com)
*/
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);
Expand All @@ -41,17 +52,16 @@ public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry r

String[] candidates = registry.getBeanDefinitionNames();

if (registry instanceof BeanFactory) {
final BeanFactory beanFactory = (BeanFactory) registry;
for (String candidate : candidates) {
if (beanFactory.isTypeMatch(candidate, beanClass)) {
return false;
}
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;
}
} else {
for (String candidate : candidates) {
BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
if (Objects.equals(beanDefinition.getBeanClassName(), 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;
}
}
Expand Down

0 comments on commit 7c6c535

Please sign in to comment.