Component Scan for custom annotation on Interface

前端 未结 3 1516
野趣味
野趣味 2021-01-18 06:32

I have a component scan configuration as this:

   @Configuration
   @ComponentScan(basePackageClasses = {ITest.class},
                  includeFilters = {@C         


        
相关标签:
3条回答
  • 2021-01-18 06:57

    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.
    0 讨论(0)
  • 2021-01-18 07:04

    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.

    0 讨论(0)
  • 2021-01-18 07:13

    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).

    0 讨论(0)
提交回复
热议问题