Component Scan for custom annotation on Interface
spring
Solution
As I said earlier, component scan is working only for concrete classes.
In order to solve my problem I have followed these steps:
- Implemented a custom `org.springframework.context.annotation.ImportBeanDefinitionRegistrar`
- Created a custom annotation `EnableJdbiRepositories` which is importing my custom importer.
- Extended ClassPathScanningCandidateComponentProvider in order to scan interfaces too. My custom importer used this class to scan interfaces too.
Problem
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 ""; } ```