【发布时间】:2017-10-21 15:03:35
【问题描述】:
这是一个更普遍的 Spring 问题,不一定是 Spring Social Facebook 问题;但是,我在这里提出问题是因为我不清楚 Spring 如何管理传递给控制器构造函数的 Facebook 引用。
我在 Spring Boot 应用程序中运行它,版本 1.5.3.RELEASE。
我在谷歌上搜索并阅读了很多关于作用域代理如何工作以及控制器是否是单例等的内容;但是对于这个特定的用例,我仍然不清楚。我担心两个或多个同时请求是否会导致一个引用干扰另一个引用。在我看来,无论使用范围代理解决方案如何,都会发生冲突。为了解决这个问题,我将 Facebook 和 ConnectionRepository 对象注入到封装在 Provider 类中的控制器构造函数中。
@Inject
public CopyGroupController(
Provider<Facebook> facebook,
Provider<ConnectionRepository> connectionRepository) {
它似乎注入了有意义的数据;但是在尝试运行此代码时:
@PostConstruct
public void init() {
ConnectionRepository repo = connectionRepository.get();
this.userManager.createOrGetUser(
repo.findPrimaryConnection(Facebook.class));
}
出现此故障:
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
这会产生更多的不确定性,因为我认为 FacebookTemplate 是作为请求或会话范围的 bean 进行管理的,并且它将作为提供程序注入,因此对象将驻留在 ThreadLocale 上;所以两个或多个并发请求不会冲突。在控制器单例构造函数中传递请求范围的 bean 对我来说也没有任何意义。所以我很困惑为什么一个用户特定的引用甚至会被传递到一个只应该被调用一次的构造函数中。
我已经尝试在构造函数中设置断点来验证是否存在冲突;但会发生与上述相同的错误。有人可以向我解释这是否是一个问题,如果是的话;如何最好,最现代的方法来解决它。
任何帮助将不胜感激。 提前谢谢你。
【问题讨论】:
标签: spring-mvc spring-boot spring-social-facebook