java - How to use @Inject in Spring MVC Controller? -
i working spring mvc controller. have 1 of controller datacontroller
.
i thinking add httpservletrequest
injectable @ top of datacontroller
class using @inject
.
@controller public class datacontroller { @inject httpservletrequest request; // .. code here @requestmapping(value = "process", method = requestmethod.get) public @responsebody dataresponse processtask(@requestparam("workflow") final string workflow) { string ipaddress = request.getremoteaddr(); system.out.println(ipaddress); }
so question - right way use @inject
? have never used @inject before trying learn whether way doing right or not? since everytime, making call processtask
method, need grab ipaddress whoever calling processtask
method.
in terms of acquiring httpservletrequest
: semantically speaking, wrong.
reason: httpservletrequest
object created when users send requests , destroyed once requested user action completed. can store way (from syntax angle) shouldn't (from semantic angle). need realize way how web application works not same desktop application (and don't observe them same angle).
suggestion:
@requestmapping(value = "process", method = requestmethod.get) public @responsebody dataresponse processtask(@requestparam("workflow") final string workflow, httpservletrequest request) {...}
in way corresponding request each time processtask
method called. (httpservletrequest object injected @requestmapping
.)
(if preserve through out session, consider use bean
suggestion: @inject private userservice userservice;
(assume have class registered called userservice
.)
Comments
Post a Comment