Possible Duplicate:
@Resource vs @Autowired
I've defined a Spring @Controller with @Scope("request") and used @Resource to get a service bean defined with @Scope("prototype"), however at every page reload I always get the same instance of the service bean.
I found out that changing @Resource to @Autowired actually works in the way that I expect, and I get a new instance of the service bean for each page reload.
Is this an expected behaviour? Am I missing some understanding on the difference between @Resource and @Autowired?
That is very strange, I would have expected them to behave the same with respect to scope at least - the only difference that I am aware of is with respect to the type of autowiring - by type for @Autowired, by name for @Resource.
I feel your issue could have been more to do with needing to specify proxyMode with @Scope annotation on your protoype bean:
@Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS) //Or ScopedProxyMode.INTERFACES
this is necessary as request scope is potentially bigger than a prototype scope. You would see the same proxy instance injected, however the proxy will ensure that when you call the method of your dependent bean it is delegated to appropriately scoped bean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With