Hello.

I came across this code and explanation from specs 9 chapter about @Context where it says about Injection, I came to the conclusion that I can not understand the idea of ​​"Injection" and therefore I do not understand why this annotation.

Injection translates as "insert", "enter", I wrote a project in java core and created classes and based on them objects, in classes I entered data into primitive types or types of a certain class, transferred them to other classes and methods, and entered data into the database. In the transition to EE, annotations and the concepts of "Injection" appeared, which I did not deal with.

 @GET @Produces{"text/plain"} public String listQueryParamNames(@Context UriInfo info) { StringBuilder buf = new StringBuilder(); for (String param: info.getQueryParameters().keySet()) { buf.append(param); buf.append("\n"); } return buf.toString(); 

There are annotations that are clear to me like:

@get @post @put @Delete @Path @PathParam @QueryParam , but @Context how much of what I read cannot understand, it is written that it is used for "Injection" in the Class field, or method, although this is what I did and without this annotation before I began to study EE.

  1. "Injection" I can not understand what it means and what is meant by this?
  2. I used to write parameters of methods only with the type, while reviewing this code, the abstract is added, for what? What does it add to the functionality or what does it affect?

    1 answer 1

    1. The introduction, in the meaning of dependency injection (DI) , is, in simple terms, a mechanism for substituting field values ​​or arguments of methods by the environment / runtime / framework / container.

      Your JAX-RS classes do not run in a vacuum. You do not even call them yourself. They are caused by the framework (Jersey / CXF / RESTEasy). This is where the values ​​are inserted before calling the method.

    2. The JAX-RS specification assumes that if an instance of a class without annotations is passed to the method, it is the body of the HTTP Request (Request Body). The framework will try to find a provider class that can convert the body of an HTTP request with a specific Content-Type into an object of your class.

      The @Context tells the framework that the value of the method argument should not be attempted from the request body, but substituted (injected) from another place. What types of arguments can be annotated with @Context , I described in response to your previous question .

    • What a tangled functional to which I can not get used to anything .. Something somewhere I did not understand and can not understand your answer .. - Maks.Burkov
    • Nofate - Do you have references to literature / info in the internet, which explains these processes? Your answer for me at this stage is like in Chinese .. - Maks.Burkov
    • martinfowler.com/articles/injection.html Books should be searched for and not necessarily in gold. On the Internet there are few articles in Russian. And in the books there. - Sergey
    • um .. well, so I understand you, Sergey, and yes, I noticed that there is little in Russian .. The question remains which books? What is the author? And if not necessarily in Java, I’m happy to hear from you about what to look for me .. - Maks.Burkov
    • one
      argc-argv.com/4_2011/article01.pdf is similar to the translation of the article by Martin Folier, the link to which he cited earlier - Sergey