Dependency Injection with annotations

I would like to create my own custom DI framework based on Java annotations, and I need a little guidance to get started. I know that it would be much easier to use one of many great frameworks like guice or spring, but for the sake of my own curiosity I would like to build my own.

I am not very familiar with annotations, so I have problems finding resources, and I really would like someone to just describe a few steps that I need to take to get started.

As mentioned above, id would like to take a factory approach and somehow label my getters using the @Resource or @Injectable annotation, and then in my business classes you can set my variable dependencies using the @Inject annotation and have the resource automatically.

Does anyone have any resource that they can pass to help me understand the process of annotation-based labeling, and then get the values ​​from a separate class based on the annotation. A little direction is all I need to get me started. And, of course, I will be happy to post a small sample code here as soon as I go, for the sake of other future readings, of course.

EDIT

Resources I use to combine:

Java Reflection: Annotations

How to find annotations in this package: stack overflow?

Scan annotations at runtime

I have not finished writing this yet, but the main list of tasks will be as follows: (for those who may be interested in doing something similar in the future)

  • In the mode of checking the execution of the class for all @Inject fields and obtaining the type of the object.

  • Scan all classes (or just a specific class package (I haven’t decided yet)) for annotated @InjectableResource methods.

  • Complete all annotated methods and find a method that returns the type of object I'm looking for.

  • Run the method and get the dependency.

It will also be useful to note that when scanning all classes I will use the Javassist library. Basically, this allows me to read bytecode information of each class without actually loading the class. Therefore, I can read lines of annotations without creating serious memory problems.

+6
source share
2 answers

Interestingly, you want to create your own. I like Google Guice - it makes the code so elegant and simple.

I used this guide , before which I found it very useful for studying annotations and how you can derive them from classes and methods.

You will need to define your own annotations that are executed using @interface. Then you will need to define some class to do the bindings, for example. where you see the interface binding in this particular class. Finally, you need some kind of logic to pull it out completely, for example. go through each class, find each annotation, and then find a suitable binding.

Consider things like lazy creation through reflection and singles. For example, Guice allows you to use a singleton, so you use only one instance of a particular class or each time you can link a new version.

Good luck

0
source
0
source

Source: https://habr.com/ru/post/909888/


All Articles