Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4152 #4169

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +29,16 @@
* @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 @@ -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;
}
}
Expand Down