Search code examples
javaspringspring-mvcreflectioncomponent-scan

Inject logic into spring component scanning process


So it is possible to use <context:component-scan ...> or @ComponentScan("org.rythmengine.spring.web") to allow spring to scan beans under certain package. The question is how can I inject my logic during the scanning process? Say I want to leverage spring's scanning to find all classes implemented a certain interface, or annotated with a certain annotation.


Solution

  • For scanning/detecting components I suggest taking a look at how <context:component-scan /> and LocalSessionFactoryBean scan for components or entities. Both use the same underlying mechanism for detecting classes.

    As for the @CacheFor annotation I would suggest leveraging Spring AOP just the same as spring uses for @Transactional and @Cacheable (to name just 2 appliances).

    Ofcourse you can place everything nicely behind a namespace just like <tx:annotation-driven /> or <cache:annotation-driven />. That is all explained in this section of the reference guide.

    Links

    1. Scanning for entities source | source
    2. Develop custom namespace reference
    3. AOP with Spring reference