Spring almost provides what I want. In Spring you can simply annotate classes with @Component and then use "context:component-scan" to have Spring automatically search for components. Then later you can create an application context and call getBean(String, Class) to get an implementation of any interface or class.
For example, if I have an interface "Mp3Service" and I want to register a new implementation called "InternetMp3Service", all I have to do is add the @Component annotation to the class definition.
The only problem with this for me is that I want to use all of this in an applet and so I get a java.lang.RuntimePermission exception for the permission "accessDeclaredMembers". I don't want to sign the applet and I don't want to have to create an explicit configuration for the beans.
What I'm looking for then is a framework/library that will go through the annotations at compile/build time and create a configuration that can then be read at run-time.
For example, if nothing comes up here and I decide it's worth the trouble, I could write my own program to go throw all the classes and look for a certain annotation and then create a spring xml configuration file. However, I thought someone might now of something already available.
Thanks.
This use case actually makes sense, but i'm sure nobody has implemented it yet. I see two ways of doing it:
a) using the Pluggable annotation Processing API. The problem here is that Spring does very advanced logic when scanning for annotations, and you will probably have to re-implement that for the annotation processor.
b) using a Main Class that does what Spring does (have a look at the source of org.springframework.context.annotation.ComponentScanBeanDefinitionParser to get an idea of where to start). Wire up the main class to a maven or ant build to have it automatically executed at compile-time. The problem here is that you will have to work on compiled classes, not on sources, so your Service Locator class won't be available at compile time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With