I have a Servlet that has a lot of existing code. I'm trying to add dependency injection into one part of it. Currently I am doing it manually:
public class AdjustBookPriceHandler extends BookRequestHandler {
@Override
public void handleRequest(RequestState requestState, RequestData requestData, Object obj) {
Book book = (Book) obj;
long newPrice = Long.parseLong(requestData.getQueryParam("price");
OfferRepository offerRepository = ((BookData) requestState.getData()).getOfferRepository();
BookPriceAdjuster priceAdjuster = getBookPriceAdjuster();
priceAdjuster.adjustPrice(newPrice);
}
protected BookPriceAdjuster getBookPriceAdjuster(RequestState requestState, RequestData requestData, Book book) {
return new BookPriceAdjuster(book, offerRepository);
}
}
Here the book and offer repository dependencies are injected into the BookPriceAdjuster through the constructor. The getBookPriceAdjuster method is there to allow classes that inherit from the AdjustBookPriceHandler to provide a different price adjuster.
I would like to start using a DI framework like Guice to reduce some of the boilerplate code that complex examples would introduce. However, I'm unsure of the best way to use it in this context.
How can I write bindings that would pull out the relevant dependencies from the "god" objects RequestState and RequestData? Or at this point would using a framework be just as complicated and messy?