1
我試圖使用從這個答案代碼多租戶添加到我的春天開機1.2.5:春季啓動範圍的問題多租戶
Setting up a MultiTenantConnectionProvider using Hibernate 4.2 and Spring 3.1.1
我得到以下異常:
Caused by: 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.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:41)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
按照上述答案的源代碼是:
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class RequestURITenantIdentifierResolver implements CurrentTenantIdentifierResolver {
@Autowired
private HttpServletRequest request;
@Override
public String resolveCurrentTenantIdentifier() {
String[] pathElements = request.getRequestURI().split("/");
String tenant = pathElements[1];
return tenant;
}
@Override
public boolean validateExistingCurrentSessions() {
return true;
}
}
我試圖改變範圍值原型:
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
,直到我嘗試在我的JpaRepository使用@Query的正常工作,如:
@Query("From Order where orderId = :id")
public Order joinWithPurchaseItems(@Param("id") Integer id);
,並拋出了同樣的異常:
Caused by: 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.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:41)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
有趣的是,使用本機查詢不會拋出此異常。
我也試過在我的應用程序類使用RequestContextListener沒有成功:
@SpringBootApplication
public class Application {
@Bean
@ConditionalOnMissingBean(RequestContextListener.class)
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
任何幫助表示讚賞。
請求範圍不起作用。刪除範圍。相反,使用RequestContextHolder來獲取RequestContext,並在上下文中使用getRequest。或者使用一個'Filter'來放入'ThreadLocal'中的東西,並讓'CurrentTenantIdentifierResolver'使用'ThreadLocal'。示例代碼可以在[這裏]找到(https://github.com/mdeinum/spring-utils/tree/master/multi-tenant)。增加的優勢是更通用的,你也可以用它來主題,資源加載等。 –
過濾器方法工作正常。謝謝!我看了一下示例代碼,它有一些有趣的功能,如果它有更多的文檔,我認爲它們會非常有用。 –