Skip to content

Commit

Permalink
add cache for scan result. (#7477) (#8057)
Browse files Browse the repository at this point in the history
* issue: Component scan two times for @service

* add cache for scan result. (#7477)

* remove redundant comments.

Co-authored-by: zhang.jie <zhangjie@pascall.xyz>
Co-authored-by: zhang.jie <zhangjie@rivamed.cn>
  • Loading branch information
3 people committed Jun 21, 2021
1 parent d16d0e1 commit 1c6e042
Showing 1 changed file with 17 additions and 6 deletions.
Expand Up @@ -17,13 +17,15 @@
package org.apache.dubbo.config.spring.context.annotation;

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 +38,10 @@
*/
public class DubboClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {

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

public DubboClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment,
ResourceLoader resourceLoader) {
Expand All @@ -57,14 +63,19 @@ public DubboClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, Envi

}

@Override
public Set<BeanDefinitionHolder> doScan(String... basePackages) {
return super.doScan(basePackages);
}

@Override
public boolean checkCandidate(String beanName, BeanDefinition beanDefinition) throws IllegalStateException {
return super.checkCandidate(beanName, beanDefinition);
}

@Override
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
Set<BeanDefinition> beanDefinitions = beanDefinitionMap.get(basePackage);
// if beanDefinitions size is null => scan
if (Objects.isNull(beanDefinitions)) {
beanDefinitions = super.findCandidateComponents(basePackage);
beanDefinitionMap.put(basePackage, beanDefinitions);
}
return beanDefinitions;
}
}

0 comments on commit 1c6e042

Please sign in to comment.