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(); }
meriton
source share