0

I have a java project containing a spring boot application called processor. This project depends on a project called rules and a project called service. Every project has the same package pattern - my.com.package.
The processor and rules projects both contain classes annotated with a custom annotation @Condition. The annotation interface is annotated with @Retention(RetentionPolicy.RUNTIME). When I scan for classes annotated with @Condition from service or processor like this

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());
            }
        }

The classes annotated with @Condition in the processor project have the correct class name(my.com.package.x.Class), but the classes annotated with @Condition in the rules project have an incorrect fully qualified class name(my.com.Class) and it only finds 2 out of 5 class names in the project that have the annotation.
If I change the argument to scanner.findCandidateComponents to the full package path in the rules project (my.com.package.rules) while scanning in either processor or service the scanner finds no candidates. If I use my.com.* as the argument it only finds the candidates in the processor project.
I saw a similar question here Using ClassPathScanningCandidateComponentProvider with multiple jar files? and the solution was to pass the class loader to the component provider. I tried getting the class loader of the class doing the scanning and passing it to the provider like this

scanner.setResourceLoader(new PathMatchingResourcePatternResolver(classLoader));

and it didn't change any results for me.

Community
  • 1
  • 1
gary69
  • 3,620
  • 6
  • 36
  • 50

1 Answers1

0

Silly mistake, the problem was I had the wrong version of the rules project defined in the pom for my processor project so it was using an older version of the code. However this

Condition condition = c.getAnnotation(Condition.class);

returned null for the classes taken from the jar, so this concerns me a little if this code isn't being run from source in my workspace.

gary69
  • 3,620
  • 6
  • 36
  • 50