Geek injection into the business layer of a Java application

I have successfully used Guice for Inject Providers in the servlet part of an existing Java web application, however I cannot access injectors through the business layer (Java servlet classes) of the application.

I read "Injecting the Injector", but for me it is more like hacking and in several places, including the Guice documentation, it says that it is not too much.

I guess my question is: where do I download the java web application so that classes without servlet / filter have access to the injector created in the class that I use to extend the GuiceServletContextListener? Is there a way to make these classes injectable without an injector injection?

Thank you and let me know if you need clarification.

Edit:

I am trying to do this with a simple logger, still in my servlets, I call:

@Inject private static org.slf4j.Logger log; 

Injection is set in MyLoggerModule as follows (which is in the createInjector call with ServletModule):

 @Override public void configure() { bindListener(Matchers.any(), new SLF4JTypeListener()); // I built my own SLF4JTypeListener... } 

All this works fine in servlets, but field injection does not work when calling a class that is not a servlet or filter.

+4
source share
2 answers

Guice does not intercept calls for new objects, so if your business layer does not already use Guice to create objects that require injection, this will require modification.

Injection is only valid for Guice treatment during the injection. Thus, starting with the base injector that you did, everything that @Inject marked that is necessary for the instance you requested will be provided to Guice as much as possible, and, in turn, during instanciation of the, further @Inject annotations will be populated by suppliers and bindings until nothing new is needed. From now on, however, you are not going to get the fields entered into servlets created outside of Guice's injection, perhaps calling new somewhere, which most likely makes your Factory object.

You will need to modify your Factory object to use suppliers instead of new ones. If you can edit them, it won't be too hard to do, since Guice can provide you with default providers for bindings.

Thus, one of the ways that your business layer could be Guice is that everything that creates servlets first creates an Injector, and then requests the creation of servlets by an injector. If this means that you will have more than one injector, then yes, this will be a problem, but only for objects that you want to be single. Thus, you can create a Factory template class for a singleton injector, or you can find where these classes are created (typed here) that create the servlets themselves (in foo), and then start with the injector there (in foo) using a single Guice injector to create these classes (such as bar), as well as modify them (type of bar), to request a provider for the servlets that they will use, instead of making calls to the new servlet.

Now that I think about it, it can be simple if it happens only once or twice for 10-20 types of servlets, or it can be difficult if there is some kind of framework that defines completely flexible behavior for that, and why.

Another option would be to avoid @Inject in fields at all times, as recommended. So now your servlets take org.slf4j.Logger as a constructive parameter. The constructor is marked with @Inject , and it assigns a parameter value to this field. Then any place where you do not use injection should break with the wrong number of parameters on a new call. Correct them by figuring out how to either get the servlet provided here, or how to get the provider for the servlet in the class.

+1
source

Not sure what you mean ... if you inject objects into your servlets / filters, these objects also have their dependencies introduced by Guice, and so on until the end.

How do you create the classes you are trying to inject this registrar into? They must be created by Guice for injection, which means new .

0
source

All Articles