0

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.

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
  • Add an `include-filter` to specify that annotation or interface. See http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-scanning-filters – M. Deinum Dec 06 '13 at 07:19
  • thx. Where can I get the collection of classes being filtered out? – Gelin Luo Dec 06 '13 at 10:20
  • Yuo cannot... Spring will use those as components . Could you be a b it more expressive in what you want? Because initially it looks like you want to add your own custom annotations to be picked up, but apparently you want something else. – M. Deinum Dec 06 '13 at 10:27
  • Here is the story. I am creating a view engine plugin for Spring, specifically http://rythmengine.org. User can define tag classes which implement a specific interface. I don't want user to add a bunch of settings in their xml configuration file to tell my plugin which classes are defined as tag, but instead, the plugin scan user's package to get their tag classes and register to the template rendering engine automatically – Gelin Luo Dec 06 '13 at 11:13
  • Then don't use component-scanning for that. You might want to take a look at the source for the `LocalSessionFactoryBean` (or its JPA counter part) on how classpath scanning and registration is done for `@Entity` classes, basically this is what you want. – M. Deinum Dec 06 '13 at 11:16
  • hmm... looks like they have user to create another scan-component-like configuration: . That makes sense since repository is usually defined in a separate package from spring beans like Controllers. I think it would a good approach for me to have user specify a base package for tag classes and implement my own scanning logic. However there is another case I want to scan for Controller method and check if there are a @CacheFor annotation on that, which I really want to inject the logic into spring scanning process, any way for that? – Gelin Luo Dec 06 '13 at 22:19
  • Those are 2 separate things. You can use the same mechanism as the `component-scan` or `LocalSessionFactoryBean` for scanning underlying they use the same mechanism. You can put this behind a namespace if you like (how to do this is explained in the reference guide). As the @CacheFor annotation you can simply use AOP for that just like it is used for `@Transactional` and `@Cacheable` from spring (to name 2 examples) – M. Deinum Dec 07 '13 at 12:53
  • Nice. Can you put all these into an answer so that I can grant you the credit? – Gelin Luo Dec 07 '13 at 20:05
  • Done, together with a couple of links to sources and resources. – M. Deinum Dec 09 '13 at 07:43

1 Answers1

1

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
Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
M. Deinum
  • 115,695
  • 22
  • 220
  • 224