java - 使用 Multi-Tenancy 时的 Spring Boot 范围问题

标签 java spring hibernate spring-mvc

我尝试使用此答案中的代码将 Multi-Tenancy 添加到我的 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;
    }
}

我尝试将范围值更改为原型(prototype):

@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)

有趣的是,使用 native 查询不会引发此异常。

我还尝试在我的 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。解析器从过滤器设置的静态字段中捕获架构名称。

关于java - 使用 Multi-Tenancy 时的 Spring Boot 范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679009/

相关文章:

JPanel 中的 Java 代码绘制

spring - RepositoryRestResource注释中的collectionResourceRel等可选元素中的 "rel"是什么意思?

java - 在测试和生产中具有不同模式的 JPA 实体

java - 如何在spring上正确地从file.properties设置hibernate?

Hibernate 映射条件多对一

java - 仅将异常处理绑定(bind)到 @ControllerAdvice 类

java - Unicode JTextArea 从 jar 中显示错误;在netbeans中很好

java - Spring - 使用@PostCostruct 时获取 NullPointErexception

java - H2 找不到适用于 jdbc 的驱动程序 :h2:mem:

java - 在 Spring Boot 中从 application.properties 配置国际化