Search in sources :

Example 1 with ComponentScan

use of org.springframework.context.annotation.ComponentScan in project dubbo-faker by moyada.

the class BeanDefinitionUtil method getBasePackages.

/**
 * 获取包扫描路径根集合
 * @param applicationContext
 * @return
 */
public static List<String> getBasePackages(ApplicationContext applicationContext) {
    List<String> backPackages = new ArrayList<>();
    Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ComponentScan.class);
    if (beans.isEmpty()) {
        return null;
    }
    Class<?>[] classes;
    String[] packages;
    for (Object instance : beans.values()) {
        Set<ComponentScan> scans = AnnotatedElementUtils.getMergedRepeatableAnnotations(instance.getClass(), ComponentScan.class);
        for (ComponentScan scan : scans) {
            classes = scan.basePackageClasses();
            packages = scan.basePackages();
            if (classes.length == 0 && packages.length == 0) {
                backPackages.add(instance.getClass().getPackage().getName());
                continue;
            }
            if (classes.length != 0) {
                for (Class<?> clazz : classes) {
                    backPackages.add(clazz.getPackage().getName());
                }
            }
            if (packages.length != 0) {
                backPackages.addAll(Arrays.asList(packages));
            }
        }
    }
    if (backPackages.isEmpty()) {
        return null;
    }
    return removeSubPackage(backPackages);
}
Also used : ComponentScan(org.springframework.context.annotation.ComponentScan)

Aggregations

ComponentScan (org.springframework.context.annotation.ComponentScan)1