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

add cache for scan result. (#7477) #8049

Merged
merged 7 commits into from Jun 21, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,14 +16,18 @@
*/
package org.apache.dubbo.config.spring.context.annotation;

import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;

import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.springframework.context.annotation.AnnotationConfigUtils.registerAnnotationConfigProcessors;

Expand All @@ -36,6 +40,11 @@
*/
public class DubboClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {

/**
* key is package to scan, value is BeanDefinition
*/
private final ConcurrentMap<String, Set<BeanDefinition>> beanDefinitionMap = new ConcurrentHashMap<>();


public DubboClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment,
ResourceLoader resourceLoader) {
Expand Down Expand Up @@ -67,4 +76,15 @@ public boolean checkCandidate(String beanName, BeanDefinition beanDefinition) th
return super.checkCandidate(beanName, beanDefinition);
}

@Override
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
Set<BeanDefinition> beanDefinitions = beanDefinitionMap.get(basePackage);
// if beanDefinitions size eq 0 => don't scan
// if beanDefinitions size is null => scan
Copy link
Member

@kylixs kylixs Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if beanDefinitions size ge 0 => don't scan

The comment is inaccurate, it is possible that the scan result is empty.
Scan only when the cache is not found.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if beanDefinitions size ge 0 => don't scan

The comment is inaccurate, it is possible that the scan result is empty.
Scan only when the cache is not found.

beanDefinitions size >= 0, means that the scan has been executed.
Because the result of the scan may be equal to 0, only beanDefinitions is null to scan.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove redundant comments.

if (Objects.isNull(beanDefinitions)) {
beanDefinitions = super.findCandidateComponents(basePackage);
beanDefinitionMap.put(basePackage, beanDefinitions);
}
return beanDefinitions;
}
}