Skip to content

Can we use ConcurrentHashMap to add BeanDefinition to ConcurrentMap instead of set #28221

Closed
@kyangcmXF

Description

@kyangcmXF

My project has one hundred thousand beans+ to initialize into spring. However every time it needs several imnutes to complete its work. I found ClassPathBeanDefinitionScanner.doScan 's code which does work one by one.
So I have a question, because it use Set to collect, can we use ConcurrentHashMap<BeanDefinition, Object> instead of it? That map's value just can use any Object such as new Object to fill it like Set inner code.

My spring version : 5.1.19.
Below is that current release code:

My Suggestion is that:

`protected Set doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
Set beanDefinitions = new LinkedHashSet<>();

    ConcurrentHashMap<BeanDefinition,Object> candidates = new ConcurrentHashMap<BeanDefinition, Object>();
    Arrays.stream(basePackages).parallel().map(x->{
        return   findCandidateComponents(basePackage);
        
    }).forEach(x->{

        candidates.put(x,new Object());
    });

    for (String basePackage : basePackages) {

        for (BeanDefinition candidate : candidates) {
            ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
            candidate.setScope(scopeMetadata.getScopeName());
            String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
            if (candidate instanceof AbstractBeanDefinition) {
                postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
            }
            if (candidate instanceof AnnotatedBeanDefinition) {
                AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
            }
            if (checkCandidate(beanName, candidate)) {
                BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
                definitionHolder =
                        AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
                beanDefinitions.add(definitionHolder);
                registerBeanDefinition(definitionHolder, this.registry);
            }
        }
    }
    return beanDefinitions;
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions