Friday, March 13, 2009

Classpath Scanning: Hidden Spring Gems

Class path scanning is not a trivial task in Java. There is no standard way to do it.
Fortunately, Spring framework can help here, as it does in many other aspects. It has an option for implicitly detecting the candidate components by scanning the classpath and matching it against the filters. But all you can do with standard Spring configuration is auto-creation of a  bunch of beans and possbly autowire them to some other bean.
What if you don't want to create beans, but just want to find specific classes and create their instances later? This can be accomplished by using ClassPathScanningCandidateComponentProvider - class that provides magic behind the Spring's class path scanning. Using it I created a simple utility class which can be used as a basis for your specific class scaners:



Here the example of specific class scanner to load classes inherited from JComponent and marked with @PreferencePage annotation:



Now we have a very powerful tool, which gives us plug in-like abilities. The program can scan the class path for specific services or GUI components and if they are present - load them. Even thaw this solution does not go as far as real plug in framework, it still allows for a greater degree of decoupling in your application.

5 comments: