package com.common.configure; import com.common.annotation.JfinalModelScan; import com.common.annotation.Table; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.EnvironmentAware; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.env.Environment; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; /** * @author 廖振钦 * @date 2022-01-14 */ @Slf4j @Data public class TableScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware { private ResourceLoader resourceLoader; private Environment environment; @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(JfinalModelScan.class.getName())); ClassPathTableScanner scanner = new ClassPathTableScanner(registry); if (resourceLoader != null) { scanner.setResourceLoader(resourceLoader); } scanner.setAnnotationClass(Table.class); List basePackages = new ArrayList(); for (String pkg : annoAttrs.getStringArray("basePackages")) { if (StringUtils.hasText(pkg)) { basePackages.add(pkg); } } scanner.registerFilters(); scanner.doScan(StringUtils.toStringArray(basePackages)); } }