/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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.Set;

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

/**
 * Dubbo {@link ClassPathBeanDefinitionScanner} that exposes some methods to be public.
 *
 * @see #doScan(String...)
 * @see #registerDefaultFilters()
 * @since 2.5.7
 */
public class DubboClassPathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {


    public DubboClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment,
                                               ResourceLoader resourceLoader) {

        super(registry, useDefaultFilters);

        setEnvironment(environment);

        setResourceLoader(resourceLoader);

        registerAnnotationConfigProcessors(registry);

    }

    public DubboClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, Environment environment,
                                               ResourceLoader resourceLoader) {

        this(registry, false, environment, resourceLoader);

    }

    @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);
    }

}

@@ -923,78 +923,8 @@
 on;%0A
-import org.springframework.beans.factory.config.BeanDefinitionHolder;%0A
 impo
@@ -1188,19 +1188,135 @@
 va.util.
-Set
+Objects;%0Aimport java.util.Set;%0Aimport java.util.concurrent.ConcurrentHashMap;%0Aimport java.util.concurrent.ConcurrentMap
 ;%0A%0Aimpor
@@ -1689,16 +1689,187 @@
 nner %7B%0A%0A
+    /**%0A     * key is package, value is BeanDefinition%0A     */%0A    private final ConcurrentMap%3CString, Set%3CBeanDefinition%3E%3E beanDefinitionMap = new ConcurrentHashMap%3C%3E();%0A
 %0A    pub
@@ -2521,142 +2521,8 @@
  %7D%0A%0A
-    @Override%0A    public Set%3CBeanDefinitionHolder%3E doScan(String... basePackages) %7B%0A        return super.doScan(basePackages);%0A    %7D%0A%0A
     
@@ -2714,10 +2714,468 @@
 %0A    %7D%0A%0A
+    @Override%0A    public Set%3CBeanDefinition%3E findCandidateComponents(String basePackage) %7B%0A        Set%3CBeanDefinition%3E beanDefinitions = beanDefinitionMap.get(basePackage);%0A        // if beanDefinitions size is null =%3E scan%0A        if (Objects.isNull(beanDefinitions)) %7B%0A            beanDefinitions = super.findCandidateComponents(basePackage);%0A            beanDefinitionMap.put(basePackage, beanDefinitions);%0A        %7D%0A        return beanDefinitions;%0A    %7D%0A
 %7D%0A

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