1

カスタム注釈を持つすべての Bean を見つけようとしているので、各 Bean の注釈プロパティを調べて、スケジュールされたジョブの助けを得ることができます。

カスタム スプリング アノテーション:

@Component
@Scope("prototype")
public @interface Importer {
    String value() default "";
    String fileRegex() default "";
}

クラス定義の例 (MyAwesomeImporterFramework は基本クラスであり、注釈はありません)

@Importer(value="spring.bean.name", fileRegex="myfile.*\\.csv")
public class MyAwesomeImporter extends MyAwesomeImporterFramework

アノテーション付きの Spring Bean を見つけるコード:

public static List<Class<?>> findBeanClasses(String packageName, Class<? extends Annotation> annotation) throws ClassNotFoundException {
    List<Class<?>> classes = new LinkedList<Class<?>>();

    ClassPathScanningCandidateComponentProvider scanner =
            new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(annotation));

    for(BeanDefinition def : scanner.findCandidateComponents(packageName)) {
        classes.add(Class.forName(def.getBeanClassName()));
    }

    return classes;
}

ファインダを使用して注釈プロパティを取得するコード。

for(Class<?> clazz : AnnotationFinder.findBeanClasses(CLASS_BASE_PACKAGE, Importer.class)) {
    // Doesn't work either:
    // Importer annotation = clazz.getAnnotation(Importer.class);
    Importer annotation = AnnotationUtils.findAnnotation(clazz, Importer.class);
    importClasses.put(annotation.fileRegex(), annotation.value());
}

ここでは、両方ともnullclazz.getAnnotation(Importer.class)を返します。AnnotationUtils.findAnnotation(clazz, Importer.class)デバッガーでコードを調べると、正しいクラスが識別されていることが示されますが、注釈のマップclazzは空です。

私は何が欠けていますか?これらのメソッドは両方とも何かを返すはずですが、実行時にアノテーションがクラスから消えてしまったようです??

4

0 に答える 0