3

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?

Community
  • 1
  • 1
stivlo
  • 83,644
  • 31
  • 142
  • 199

1 Answers1

1

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.

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Thanks Biju, adding proxyMode = ScopedProxyMode.INTERFACES (in my case I have interfaces), it works. I might be slow, but I still don't understand why the different behaviour. I also feel that the question indicated by @GeorgeStocker as identical, it's not, since is not mentioning scopes at all. – stivlo Sep 23 '12 at 14:02