All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codingapi.springboot.framework.registrar.RegisterBeanScanner Maven / Gradle / Ivy

There is a newer version: 3.2.6
Show newest version
package com.codingapi.springboot.framework.registrar;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.data.util.AnnotatedTypeScanner;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
public class RegisterBeanScanner {

    private final AnnotationMetadata importingClassMetadata;
    private final Class handlerClass;
    private final List scannerPackageNames;

    public RegisterBeanScanner(AnnotationMetadata importingClassMetadata, Class handlerClass) throws ClassNotFoundException {
        this.importingClassMetadata = importingClassMetadata;
        this.handlerClass = handlerClass;
        this.scannerPackageNames = new ArrayList<>();
        this.loadScannerPackages();
    }

    private void loadScannerPackages() throws ClassNotFoundException {
        final Map attributes = importingClassMetadata.getAnnotationAttributes(SpringBootApplication.class.getName());
        if (attributes == null) {
            return;
        }

        String defaultPackage = (Class.forName(importingClassMetadata.getClassName())).getPackage().getName();
        addPackage(defaultPackage);

        String[] scanBasePackages = (String[]) attributes.get("scanBasePackages");

        if (scanBasePackages != null) {
            for (String basePackages : scanBasePackages) {
                addPackage(basePackages);
            }
        }

        log.debug("scannerPackageNames:{}", scannerPackageNames);
    }


    public List findBeanDefinitions() {
        ClassPathScanningCandidateComponentProvider pathScanningCandidateComponentProvider = new ClassPathScanningCandidateComponentProvider(false);
        pathScanningCandidateComponentProvider.addIncludeFilter(new AnnotationTypeFilter(handlerClass, true, true));

        List candidateComponents = new ArrayList<>();
        for (String packageName : scannerPackageNames) {
            candidateComponents.addAll(pathScanningCandidateComponentProvider.findCandidateComponents(packageName));
        }
        return candidateComponents;
    }

    public Set> findTypes() {
        AnnotatedTypeScanner annotatedTypeScanner = new AnnotatedTypeScanner(handlerClass);
        return annotatedTypeScanner.findTypes(scannerPackageNames);
    }


    private String appendPackageChildren(String packageName) {
        if (!packageName.endsWith(".*") || !packageName.endsWith(".**")) {
            packageName = String.format("%s.**", packageName);
        }
        return packageName;
    }


    private void addPackage(String packageName) {
        if (packageName != null) {
            scannerPackageNames.add(appendPackageChildren(packageName));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy