使用多租户时Spring引导范围问题

时间:2015-09-20 11:46:50

标签: java spring hibernate spring-mvc

我尝试使用此答案中的代码将多租户添加到我的spring boot 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)

有趣的是,使用本机查询不会抛出此异常。

我还尝试在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);
    }
}

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

正如评论中所建议的,我使用了过滤器,并从那里捕获了URL。解析器从过滤器设置的静态字段中捕获模式名称。

相关问题