注册器工具类
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AspectJTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
public class CustomeImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
private String expression;
public CustomeImportBeanDefinitionRegistrar(){
try {
Properties loadAllProperties = PropertiesLoaderUtils.loadAllProperties("****.properties");
expression = loadAllProperties.getProperty("****");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
String[] basePackages = null;
if (importingClassMetadata.hasAnnotation(ComponentScan.class.getName())) {
Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
basePackages = (String[]) annotationAttributes.get("basePackages");
}
if (basePackages == null || basePackages.length == 0) {
String basePackage = null;
try {
basePackage = Class.forName(importingClassMetadata.getClassName()).getPackage().getName();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
basePackages = new String[] {basePackage};
}
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false);
TypeFilter typeFilter = new AspectJTypeFilter(expression,this.getClass().getClassLoader());
scanner.addIncludeFilter(typeFilter);
scanner.scan(basePackages);
}
}