Spring加载早期获取BasePackage

时间:2019-09-03 12:46:49   收藏:0   阅读:103
public class GetBasePackage {
    private Class<? extends Annotation> annotation;

    public GetBasePackage(Class<? extends Annotation> annotation) {
        this.annotation = annotation;
    }

    public Stream getBasePackage(AnnotationMetadata annotationMetadata){
        Map<String, Object> annotationAttributes = annotationMetadata.getAnnotationAttributes(annotation.getName());
        AnnotationAttributes attributes = new AnnotationAttributes(annotationAttributes);
        String[] value = attributes.getStringArray("value");//annotationg中的注解
        String[] basePackages = attributes.getStringArray("basePackages");//annotationg中的注解
        String[] entityPaths = attributes.getStringArray("entityPath");//annotationg中的注解
        //没配注解
        if (value.length == 0 && basePackages.length == 0 && entityPaths.length == 0) {
            String className = annotationMetadata.getClassName();
            return  Stream.of(ClassUtils.getPackageName(className));
        }
        //配了注解
        Set<String> packages = new HashSet<>();
        packages.addAll(Arrays.asList(value));
        packages.addAll(Arrays.asList(basePackages));
        packages.addAll(Arrays.asList(entityPaths));
        return Stream.of(packages);
    }
}
GetBasePackage getBasePackage = new GetBasePackage(EnableESTools.class);
getBasePackage.getBasePackage(annotationMetadata).forEach((s) -> {
    System.out.println(s);
});

 

原文:https://www.cnblogs.com/zxporz/p/11452026.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!