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

issue: Component scan two times for @Service #7477

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -162,13 +162,9 @@ private void registerServiceBeans(Set<String> packagesToScan, BeanDefinitionRegi
});

for (String packageToScan : packagesToScan) {

// Registers @Service Bean first
scanner.scan(packageToScan);


// Finds all BeanDefinitionHolders of @Service whether @ComponentScan scans or not.
Set<BeanDefinitionHolder> beanDefinitionHolders =
findServiceBeanDefinitionHolders(scanner, packageToScan, registry, beanNameGenerator);
Set<BeanDefinitionHolder> beanDefinitionHolders = scanner.doScan(registry, beanNameGenerator, packageToScan);

if (!CollectionUtils.isEmpty(beanDefinitionHolders)) {

Expand Down
Expand Up @@ -16,14 +16,20 @@
*/
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.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;

import java.util.LinkedHashSet;
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 +42,7 @@
*/
public class DubboClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {

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 +74,35 @@ 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 (CollectionUtils.isEmpty(beanDefinitions)) {
beanDefinitions = super.findCandidateComponents(basePackage);
beanDefinitionMap.put(basePackage, beanDefinitions);
}
return beanDefinitions;
}

/**
* Registers @Service Bean and Finds all BeanDefinitionHolders of @Service
*
* @param registry BeanDefinitionRegistry
* @param beanNameGenerator BeanNameGenerator
* @param basePackage basePackage
* @return all BeanDefinitionHolders of @Service
*/
public Set<BeanDefinitionHolder> doScan(BeanDefinitionRegistry registry, BeanNameGenerator beanNameGenerator, String basePackage) {
// Registers @Service Bean first
super.doScan(basePackage);
Set<BeanDefinition> beanDefinitions = findCandidateComponents(basePackage);
Copy link
Member

Choose a reason for hiding this comment

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

super.doScan(basePackage): will do first scanning
findCandidateComponents() : will do second scanning
This did not reduce scanning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

super.doScan(basePackage): Call method findCandidateComponents() for first time run, this method will cache scan result.
findCandidateComponents(basePackage): this method run for the second time and will use cached results instead of scanning.
So this reduce scanning for the second time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rewrite method findCandidateComponents for DubboClassPathBeanDefinitionScanner.

Copy link
Member

Choose a reason for hiding this comment

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

I think just cache Set<BeanDefinitionHolder> in findCandidateComponents method is enough, do not need add new doScan method

Set<BeanDefinitionHolder> beanDefinitionHolders = new LinkedHashSet<BeanDefinitionHolder>(beanDefinitions.size());
for (BeanDefinition beanDefinition : beanDefinitions) {
String beanName = beanNameGenerator.generateBeanName(beanDefinition, registry);
BeanDefinitionHolder beanDefinitionHolder = new BeanDefinitionHolder(beanDefinition, beanName);
beanDefinitionHolders.add(beanDefinitionHolder);
}
return beanDefinitionHolders;
}

}