I am working on a task to develop a simple web framework without any configuration. Below is the details:
We need to achieve the two goal:
1. we supply an interface, when a class implement the interface, this class is a http service class via http to invoke a default method.
2. we supply an Annotation, when a method in any class refers to this Annotation, this method will auto bind to a web service.
the problem is:
1.with out configuration as xml or properties, how can I find all the classes implement my interface in the runtime classpath?
2.the same as the first one, how can I find all the classes refer to my Annotation?
Thanks very much.
Mama always said, "Life is like a box of chocolates; you never know what you're gonna get. And then you have to get medieval on somebody's buttocks."
Joined: Jul 24, 2007
maybe, from the classloader, it is impossible to find all the implementation.
The problem with using an interface is that you can't find out which class implements it until you instantiate it. (At least not with the standard Java API; there are libraries like BCEL and Javassist that can provide that information by looking directly at the class file.)
I'd say that using annotations is preferable. If the annotation has a RetentionPolicy of RUNTIME then your code can find out about it at -well- runtime by looking at the Class object of the class in question. You may want to iterate through the contents of the WEB-INF/classes and WEB-INF/lib directories at startup time to find all such classes.
Check out the open source Stripes web framework as an example of how such configuration can be done completely based on annotations. I find it very convenient to use.
Ping & DNS - updated with new look and Ping home screen widget
Joined: Jul 24, 2007
I am seeing Stripes now, it looks well.
If it is possible to use annotations, the next is I need to try to integrate Google Guice as DI and Factory.
Joined: Mar 22, 2005
The one book available on Stripes has a section on how to integrate Guice and Stripes. Code example "email_33" -downloadable from that web site- is a ready-to-run web app that shows this in action (although it may be a bit hard to follow w/o reading the book chapter). But if you're serious about Stripes, then $23 for a PDF or $36 for paper is well spent; I can recommend the book.