I encountered with this question in one of the interviews. Can we inject request scoped bean to session scoped bean and vice versa.Explain?
Asked
Active
Viewed 3,043 times
3
-
1Inject yes... Contextually Inject, no – user489041 Mar 20 '15 at 14:41
-
You can inject "bigger" scoped beans (a SessionScoped into a RequestScoped) but not otherwise because you may have a method running without a request, e.g. – Dominik Sandjaja Mar 20 '15 at 14:47
2 Answers
0
When you take a look and Spring Documentation, you need to use proxying for these web scopes.
If you are using it correctly, there is proxy instance injected into new bean. And when you perform the call, proxy will resolve it to current session/scope instance of the bean.
So it should work fine, you just need to know at what stage the instance behind proxy is replaced.
Injecting request scope bean into session scope bean doesn't make sense, because there will always be new request for that session.

luboskrnac
- 23,973
- 10
- 81
- 92
0
It is possible to inject @RequestScope bean into @SessionScope.
Explanation is given here https://stackoverflow.com/a/26311504/4820148

Community
- 1
- 1

Jakub Hesoun
- 108
- 1
- 6