Sep 25th, 2012, 10:23 AM
@Scope request and prototype in 3.1.2 with struts2 controllers
I am trying to upgrade from 3.0.3 to 3.1.2. It is a struts2 app, with a controller/action marked as @Scope("request"). A new action object is created on each request. The action bean has a SearchCriteria object injected into it. The SearchCriteria class is marked with @Scope("prototype"). So I'd expect a new one to get created on every request, which is how it worked with spring 3.0.3. But now first time it sees it as a prototype and creates a new one, but from then on it uses a cached version. I've tried to debug it, but am getting no where fast.
Maybe there is a new configuration setting I'm not aware of? Or any other ideas would be appreciated.
Sep 27th, 2012, 06:16 AM
The fastest and straight forward resolution to this would be to give the scope REQUEST to the SearchCriteria bean as well. Thus every request would be provided with a new instance of SearchCriteria every time. I could have suggested using <aop:scoped-proxy> on the action if your action is getting injected in your SearchCriteria but you can't do this as your SearchCriteria is prototype scope and AOP Scoped proxies doesn't work on singleton or prototype scoped beans and would throw BeanCreationEXception. You may read here
Sep 27th, 2012, 03:39 PM
I did try setting both to scope of request, without success.
Sep 27th, 2012, 08:17 PM
Trust me on this. Your DEV environment might be doing this crazy behavior. Did you restart your Eclipse/STS and/or application server? If on windows, I may also recommend to restart Windows. Did you confirm that your changes are present in deployment directory of application server after you set the scope of both to REQUEST? There's no reason for it not to work. One more thing to make sure is that both the beans should be declared inside a WebApplicationContext and not the FileSystemApplicationContext or ClasspathApplicationContext? Can you paste the spring configuration as seen by your dispatcher-servlet? That would help in analyzing the issue. Actually request scope is only available inside a WebApplicationContext.