簡體   English   中英

使用多租戶時的Spring Boot作用域問題

[英]Spring boot scope issue when using multitenancy

我嘗試使用此答案中的代碼將多租戶添加到我的Spring Boot 1.2.5中:

使用Hibernate 4.2和Spring 3.1.1設置MultiTenantConnectionProvider

我得到以下異常:

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)

有趣的是,使用本機查詢不會引發此異常。

我還嘗試在Application類中使用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);
    }
}

任何幫助表示贊賞。

如評論中所建議,我改用了過濾器,並從那里捕獲了URL。 解析器從過濾器設置的靜態字段中捕獲模式名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM