8

I have a component scan configuration as this:

   @Configuration
   @ComponentScan(basePackageClasses = {ITest.class},
                  includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = JdbiRepository.class)})
   public class MyConfig {

   }

Basically I would like to create scan interface which has JdbiRepository annotation

@JdbiRepository
public interface ITest {
  Integer deleteUserSession(String id);
}

And I would like to create proxy implementation of my interfaces. For this purpose I registered a custom SmartInstantiationAwareBeanPostProcessor which is basically creating necessary instances but the configuration above does not scan interfaces which has JdbiRepository annotation.

How can I scan interfaces by custom annotation?

Edit:

It seems that org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#isCandidateComponent is accepting only concrete classes.

/**
 * Determine whether the given bean definition qualifies as candidate.
 * <p>The default implementation checks whether the class is concrete
 * (i.e. not abstract and not an interface). Can be overridden in subclasses.
 * @param beanDefinition the bean definition to check
 * @return whether the bean definition qualifies as a candidate component
 */
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
    return (beanDefinition.getMetadata().isConcrete() && beanDefinition.getMetadata().isIndependent());
}

Edit:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface JdbiRepository {

   /**
    * The value may indicate a suggestion for a logical component name,
    * to be turned into a Spring bean in case of an autodetected component.
    * @return the suggested component name, if any
    */
   String value() default "";
}
omnomnom
  • 8,911
  • 4
  • 41
  • 50
Cemo
  • 5,370
  • 10
  • 50
  • 82
  • How is defined your annotation ? I think you must have Spring's `@Component` annotation on `@JdbiRepository` – Guillaume Darmont Jul 04 '13 at 20:14
  • @GuillaumeDarmont it has Component annotation. But I guess that component scanning is working for just concrete classes. – Cemo Jul 04 '13 at 20:17
  • Hmm. I did not pay attention to that. But since `isCandidateComponent` can be overriden, you may subclass `ClassPathScanningCandidateComponentProvider`. But I don't know how to integrate it into Spring ApplicationContext loading (self registering ? explicit declaration ?...) – Guillaume Darmont Jul 04 '13 at 20:32
  • It will be my last option but before tomorrow I will try another approach as Spring Data project. They are registering components by interfaces. I will give a try, thanks – Cemo Jul 04 '13 at 20:35

3 Answers3

6

Creating a Dummy implementation seems quite hacky to me and all the steps Cemo mentioned require a lot of effort. But extending ClassPathScanningCandidateComponentProvider is the fastest way:

ClassPathScanningCandidateComponentProvider scanningProvider = new ClassPathScanningCandidateComponentProvider(false) {
    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        return true;
    }
};

Now you´re also able to scan for Interfaces with (custom) Annotations with Spring - which also works in Spring Boot fat jar environments, where the fast-classpath-scanner (which is mentioned in this so q&a) could have some limitations.

Community
  • 1
  • 1
jonashackt
  • 12,022
  • 5
  • 67
  • 124
  • 1
    Wait for spring 5.0 :) I had filed an isue and it was scheduled to be resolved in spring 5.0. – Cemo Jan 06 '17 at 11:46
3

As I said earlier, component scan is working only for concrete classes.

In order to solve my problem I have followed these steps:

  1. Implemented a custom org.springframework.context.annotation.ImportBeanDefinitionRegistrar
  2. Created a custom annotation EnableJdbiRepositories which is importing my custom importer.
  3. Extended ClassPathScanningCandidateComponentProvider in order to scan interfaces too. My custom importer used this class to scan interfaces too.
Cemo
  • 5,370
  • 10
  • 50
  • 82
  • 1
    You might have registered proxy implementation of `JdbiRepository` annotated interfaces as beans. Could you please guide how did you do it in your implementation of `ImportBeanDefinitionRegistrar`. Could you please share some sample code? – Bilal Mirza Jul 18 '16 at 04:33
  • 1
    This is a too old question but the answer here is https://jira.spring.io/browse/SPR-11663 :) It will be resolved in M2. – Cemo Jul 21 '16 at 12:12
0

Instead of scanning an annotated interface, you can create a dummy implementation and annotate it accordingly:

@JdbiRepository
public class DummyTest implements ITest {
  public Integer deleteUserSession(String id) {
    // nothing here, just being dummy
  }
}

Then, scan the dummy implementation basePackageClasses = {DummyTest.class}.

It's a bit of a workaround, but very simple and good enough for test purposes (as seems to be here).

leaqui
  • 533
  • 6
  • 22
yair
  • 8,945
  • 4
  • 31
  • 50