java - Spring MVC Scoped Bean Dependencies and Race Conditions -
i have serious doubts regarding scope bean dependencies because of lack of spring knowledge.
i have read reference manual @ 3.5.4.5 scoped beans dependencies , have implemented sucessfully example it.
however before going further more, wanted share concerns.
let me share use case , little implementation details
for each user request create city per user.
@configuration public class cityfactory{ @bean(name = {"currentcity" , "loggedincity"}) @scope(value = webapplicationcontext.scope_request,proxymode = scopedproxymode.target_class) @autowired public citybean getcitybean(httpservletrequest request) { return cityutil.findcitywithhostname(request.getservername()); }
for each request want inject city singleton scoped controller default scope controller.
@requestmapping("/demo") @controller public class democontroller { @autowired citybean city; @requestmapping(value = "/hello/{name}", method = requestmethod.get) public modelandview helloworld(@pathvariable("name") string name, model model) { map<string, object> mymodel = new hashmap<string, object>(); model.addattribute("hello", name); model.addattribute("test", "test in " + city.getdomainname() + " !!! "); return new modelandview("v3/test", "m", model); } }
my questions:
1) there race condition? afraid of context switches destroy application in multi request environment.
2) aware of solution creating controller per request more error prone current solution. because developer can forget scoping controllers make request.
how can make controllers globally request scope? because of being little curious.
thanks...
no race conditions - each request has own thread
but think there's easier way want. can have citybean
:
@service public class citybean { public string getdomainname(string servername) { // obtain name based on server name } }
and in controller:
@autowired citybean bean
- pass
httpservletrequest
argument method, , callcitybean.getdomainname(request.getservername());
(if use orm, perhaps you'll have city
entity, can fetch , pass around, beware of lazy collections)
Comments
Post a Comment