Skip to content

Commit 95652b1

Browse files
committed
Fix #4152
`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
1 parent a62881d commit 95652b1

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Apollo 2.0.0
2727
* [Split helm chart into another repo](https://github.com/apolloconfig/apollo/pull/4125)
2828
* [fix gray publish refresh item status](https://github.com/apolloconfig/apollo/pull/4128)
2929
* [Support only show difference keys when compare namespace](https://github.com/apolloconfig/apollo/pull/4165)
30+
* [Fix the issue that property placeholder doesn't work for dubbo reference beans](https://github.com/apolloconfig/apollo/pull/4175)
3031

3132
------------------
3233
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/util/BeanRegistrationUtil.java

+21-11
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,27 @@
1818

1919
import java.util.Map;
2020
import java.util.Objects;
21+
import java.util.concurrent.ConcurrentHashMap;
2122

22-
import org.springframework.beans.factory.BeanFactory;
2323
import org.springframework.beans.factory.config.BeanDefinition;
2424
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2525
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
26+
import org.springframework.core.type.MethodMetadata;
2627

2728
/**
2829
* @author Jason Song([email protected])
2930
*/
3031
public class BeanRegistrationUtil {
32+
// reserved bean definitions, we should consider drop this if we will upgrade Spring version
33+
private static final Map<String, String> RESERVED_BEAN_DEFINITIONS = new ConcurrentHashMap<>();
34+
35+
static {
36+
RESERVED_BEAN_DEFINITIONS.put(
37+
"org.springframework.context.support.PropertySourcesPlaceholderConfigurer",
38+
"org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer"
39+
);
40+
}
41+
3142
public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry registry, String beanName,
3243
Class<?> beanClass) {
3344
return registerBeanDefinitionIfNotExists(registry, beanName, beanClass, null);
@@ -41,17 +52,16 @@ public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry r
4152

4253
String[] candidates = registry.getBeanDefinitionNames();
4354

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-
}
55+
String reservedBeanDefinition = RESERVED_BEAN_DEFINITIONS.get(beanClass.getName());
56+
for (String candidate : candidates) {
57+
BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
58+
if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) {
59+
return false;
5060
}
51-
} else {
52-
for (String candidate : candidates) {
53-
BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
54-
if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) {
61+
62+
if (reservedBeanDefinition != null && beanDefinition.getSource() != null && beanDefinition.getSource() instanceof MethodMetadata) {
63+
MethodMetadata metadata = (MethodMetadata) beanDefinition.getSource();
64+
if (Objects.equals(reservedBeanDefinition, String.format("%s#%s", metadata.getDeclaringClassName(), metadata.getMethodName()))) {
5565
return false;
5666
}
5767
}

0 commit comments

Comments
 (0)