Skip to content

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

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

Closed
kyangcmXF opened this issue Mar 23, 2022 · 1 comment
Labels
status: duplicate A duplicate of another issue

Comments

@kyangcmXF
Copy link

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;
}`
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 23, 2022
@snicoll
Copy link
Member

snicoll commented Mar 23, 2022

Thanks for the suggestion but post-processing bean definitions asynchronously is not possible at the moment. See also #13410

@snicoll snicoll closed this as completed Mar 23, 2022
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants