mercredi 25 mars 2015

Inject a new instance of an object into each request handler

I have a lot of Spring RestControllers with methods annotated with RequestMapping. I now would like to inject a custom object into these RequestMapping methods, and create an custom instance for each request.


I would like to write something like the following:



@RequestMapping("/search")
public SomeReturnObject foobar(@RequestParam("query") String query, MyRequestFoo foo) {
// ...
}


Now I would like to create a mechanism, where each call to that method (i.e. each request) get a new instance of MyRequestFoo created and injected into the method. If this would work better with an parameter annotation instead of injecting by type, that would also be okay (e.g. @MyRequestInject MyRequestFoo foo).


I need to know if I can create now a method that creates a new instance of MyRequestFoo especially for that request, like the following:



public MyRequestFoo createRequestInstanceSomehow(HttpServletRequest request) {
// extract some values from the HttpServletRequest and create a
// new MyRequestFoo instance from that and return it
}


Is this possible by any means to create such a mechanism, so that I can inject custom per request objects into my request handling methods?


Aucun commentaire:

Enregistrer un commentaire