processor
というスプリングブートアプリケーションを含むJavaプロジェクトがあります。このプロジェクトは、rules
というプロジェクトとservice
というプロジェクトに依存しています。すべてのプロジェクトは同じパッケージパターン(my.com.package
)を持っています。
processor
およびrules
プロジェクトには、カスタム注釈@Condition
で注釈が付けられたクラスが含まれています。アノテーションインタフェースには@Retention(RetentionPolicy.RUNTIME)
と注釈が付けられています。私はservice
から@Condition
またはprocessor
ClassPathScanningCandidateComponentProvider.findCandidateComponentsのクラス名が間違っています
private ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Condition.class));
for (BeanDefinition bd : scanner.findCandidateComponents("my.com")) {
try {
Class<?> c = Class.forName(bd.getBeanClassName());
Condition condition = c.getAnnotation(Condition.class);
register(condition);
} catch (ClassNotFoundException | IOException e) {
logger.error(e.getLocalizedMessage());
}
}
で注釈を付けたクラスをスキャンするとprocessor
プロジェクトに@Condition
で注釈を付けたクラスは、正しいクラス名(my.com.package.x.Class
)を持っていますが、rules
プロジェクトに@Condition
でアノテートクラスが持っています間違った完全修飾クラス名(my.com.Class
)であり、注釈を持つプロジェクト内の5つのクラス名のうち2つしか見つかりません。
processor
またはservice
のいずれかをスキャンしている間に、引数をscanner.findCandidateComponents
に変更してrules
プロジェクト(my.com.package.rules
)の完全なパッケージパスに変更すると、スキャナは候補を検出しません。引数としてmy.com.*
を使用すると、processor
プロジェクトの候補のみが検索されます。
私は同様の質問Using ClassPathScanningCandidateComponentProvider with multiple jar files?を見ました。解決策はクラスローダをコンポーネントプロバイダに渡すことでした。クラスのクラスローダーをスキャンしてこのようなプロバイダに渡そうとしました
scanner.setResourceLoader(new PathMatchingResourcePatternResolver(classLoader));
これは私のために結果を変更しませんでした。