I have one small issue with creating a new bean. Basically as per request, I get some parameters, which needs to be passed to a bean. Below I am instantiating ControllerService for each request. Rather I would like it to be a bean with scope=protype. So that I get a fresh object for every request.
But then how do i set the 2 properties (kpiName, kpiInput) that I am sending via constructors in the bean??
@Autowired
@Qualifier("serviceManager")
Cleanser serviceManager;
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody
String getKPIResult(@RequestParam("kpiName") String kpiName,
@RequestParam("kpiInput") String kpiInput) {
return serviceManager.checkAndExecute(new ControllerService(kpiName, kpiInput));
}
In situations like this where you’re going against the grain of Spring, I’d suggest that perhaps you’re doing something in a way that’s not considered best practice. Without more context it’s hard to see though.
Spring Social uses a
requestscope bean to embody a repository for a specific user. I’ve now idea why as it’s a horribly inefficient way of doing things, and much less understandable IMHO.You can see here the use of
factory-beanandfactory-methodto declare a class/method to call when wanting an instance of your class. The constructor argument is passed using SpEL. I’m not quite sure how you’d achieve this with Spring MVC responding to web requests, but I’m fairly sure you could use Spring Integration to pass a message and use SpEL to grab headers/payload form that message to pass to the constructor.Again though, I’d really question your design pattern here – a more usual SOA idiom is to create services on startup, and have them as stateless as possible from there-on in, rather than create an instance with specific state for each request. Best of luck!