6

I would like to build my own custom DI framework based on Java annotations and I need a little direction to get started. I know it would be much easier to use one of the many wonderful frameworks out there such as guice or spring, but for the sake of my own curiosity, i'd like to build my own.

I'm not very familiar with annotations, so i'm having a bit of trouble finding resources and would really appreciate someone just sort of spelling out a few of the steps i'll need to take to get started.

As fore mentioned, id like to take a factory approach and somehow label my getters with an @Resource or @Injectable type annotation, and then in my business classes be able to set my variable dependencies with an @Inject annotation and have the resource automatically available.

Does anyone have any sort of resource they can pass along to help me understand the process of tagging methods based on annotations and then retrieving values from a separate class based on an annotation. A little direction is all I need, something to get me started. And of course i'll be happy to post a little code sample here once I get going, for the sake of others future reading of course.

EDIT

The resources I am using to put this together:

Java Reflection: Annotations

How to find annotations in a given package: Stack Overflow ?

Scanning Annotations at Runtime

I have not actually finished writing this yet, but the basic task list is going to be as follows (for anyone who might be interested in doing something similar in the future)

  1. At class runtime scan for all @Inject fields and get object type.

  2. Scan all classes (or just a specific package of classes (I haven't decided yet)) for annotated methods @InjectableResource.

  3. Loop all annotated methods and find the method that returns the object type I am looking for.

  4. Run the method and get the dependency.

It will also be helpful to note that when scanning all the classes I will be using a library called Javassist. Basically what this does is allows me to read the bytecode information of each class without actually loading the class. So I can read the annotation strings without creating serious memory problems.

Community
  • 1
  • 1
ryandlf
  • 27,155
  • 37
  • 106
  • 162
  • You can start off with the standard [Oracle Annotation Trail](http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html). You will need some way of scanning your entire classpath for resources, one way would be using a tool like [Google Reflections](http://code.google.com/p/reflections/). And finally you will need some way of manipulating classes, for example with [Javassist](http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/). – Perception Mar 03 '12 at 17:18

2 Answers2

0

Interesting that you want to build your own. I love Google Guice - it makes code so elegant and simple.

I've used this guide before which I found pretty useful for learning about annotations and how you can pull them out of classes and methods.

You will have to define your own Annotations which is done using @interface. Then you will have to define some kind of class for doing bindings e.g. where you see an interface bind in this concrete class. Finally, you will need some logic to pull it altogether e.g. go through each class, find each annotation, and then find a suitable binding.

Give consideration to things like lazy instantiation through Reflections and singletons. Guice, for example, allows you to use a singleton so your only using one instance of the concrete class, or you can bind a new version each time.

Good luck!

christophmccann
  • 4,181
  • 7
  • 42
  • 66
  • Great advice so far. The resource you linked is helpful. Any insight on how to actually bind a method (a getter that creates the resource in my case) to an annotation? I get how to create a custom annotation and have access variables set inside that annotation, but how do I go about having the @Inject point me to the factory class and specific method? – ryandlf Mar 03 '12 at 17:27
  • I haven't built my own dependency injection framework, so my way would just be to go about how Guice does it. You will need a bind method which takes the interface, the annotation, and the concrete class. Guice's way of doing this is to have an AbstractModule which is instantiated by concrete modules defined by the users which define all the bindings. See here: http://code.google.com/p/google-guice/wiki/BindingAnnotations – christophmccann Mar 03 '12 at 17:32
0

Have a look at the following methods:

java/lang/Class.html#getAnnotation(java.lang.Class)

java/lang/Class.html#getAnnotations()

java/lang/Class.html#getDeclaredAnnotations()

Methods of the same name also exist for the java/lang/reflect/Method, java/lang/reflect/Field and java/lang/reflect/Constructor classes.

So in order to use these sorts of methods, you need to know a bit about Java reflection.

Paul Grime
  • 14,970
  • 4
  • 36
  • 58