How are dependency introductions introduced manually?

How can we manually enter an object without using a container. I did something similar through reflection as follows.

Class actionClass = Class.forName("SampleClass"); Object actionObject = actionClass.newInstance(); Method reqMethod = actionClass.getMethod("setRequest", HttpServletRequest.class); reqMethod.invoke(actionObject,request); 

Is this the right way to do DI?

I intend to pass the request object to various controller classes dynamically from the dispatcher filter, where we get the request and response objects. I'm afraid of reflection. Is there a replacement for doing DI?

+6
java reflection dependency-injection
source share
5 answers

Injection injection is nothing more than providing a class with its dependencies, rather than finding them yourself (through singleton / search queries, etc.). So you can do this in code trivially this way:

 DatabaseConnection dc = new DatabaseConnection(); MyDAO dao = new MyDAO(dc); 

(pseudo code). This is where MyDAO introduces a database connection. If this database connection implements an interface, you can easily mock it during testing.

+5
source share

Well, when you install one object into another object using the setter method or through the constructor, it is also a dependency injection. Dependency injection means only creating relationships (dependencies) in objects.

Using reflection, like you, is another form of it.

+2
source share

Why are you using reflection? Why not just:

 SampleClass action = new SampleClass(); action.setRequest(request); 

This does the same thing, but more readable, allows the compiler to check whether the types and methods actually exist, provide you with Javadoc for the called method, allow your IDE to help with refactoring, ...

And still, this is an injection of dependencies, because the action does not search for its request, but receives the request during initialization.

Edit : Please Thorbjørn show how this action will be used. He himself will be introduced (using the setter) into any component that uses the action. The component will then use the object with nested actions.

 SampleClass action = new SampleClass(); action.setRequest(request); Servlet servlet = new ActionBasedServlet(); servlet.setAction(action); 

If the servlet meant to live longer than the action , that is, it should use a fresh action every time it needs it, instead, you can set set-inject ActionFactory to the servlet instead.

In this particular case, I would question whether the action should really contain the request as part of its state or whether it can be unchanged and just act on the request passed by the parameter of the Servlet method. In this case, boot time initialization would do:

 SampleClass action = new SampleClass(); Servlet servlet = new ActionBasedServlet(); servlet.setAction(action); 

and ActionBasedServlet will determine

 public void serve(Request req, Response resp) { foo(); action.act(req, resp); bar(); } 
+1
source share

Dependency injection implies that you get correctly initialized links that appear "by magic."

You call the setRequest () method with the request object, but DI often also allows you to set fields without calling methods.

Guice does not as such require a container, but uses the magic of a class loader launched in the main method. Would this be helpful to you?

0
source share

Spring framework is one of the most popular DI implementations. It is also open source. You can check the org.springframeowrk.beans.BeanUtils class, in particular the copyProperties methods (all 4 of them) for examples of how to do this. For more information, you can also see the org.springframework.beans.factory.BeanFactory class hierarchy for different strategies.

0
source share

All Articles