我尝试使用此答案中的代码将 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/