How to check for Request Scope availability in Spring?

谁说我不能喝 提交于 2019-12-03 23:36:52
Sergey Shcherbakov

You can use the if check described here

SPRING - Get current scope

if (RequestContextHolder.getRequestAttributes() != null) 
    // request thread

instead of catching exceptions. Sometimes that looks like the simplest solution.

You can inject a Provider<MyRequestScopedBean> and catch the exception when calling the get method but you should rethink your design. If you feel strongly about it you should probably have two beans with different qualifier

Edit

On second thought, if you are using java configuration, @Scope("prototype") your @Bean method and make your decision there, you can get a handle on request context via RequestContextHolder if available. But I strongly recommend you rethink your design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!