Spring 获取不带注释参数的resteasy servlet上下文

Spring 获取不带注释参数的resteasy servlet上下文,spring,jsf-2,resteasy,Spring,Jsf 2,Resteasy,快速项目说明:我们有一个基于JSF2+Spring的动态数据源构建的应用程序。数据引用控件是通过spring配置创建的: <bean id="dataSource" class="com.xxxx.xxxx.CustomerRoutingDataSource"> .... 上面调用的CustomerContextHolder如下所示: public class CustomerContextHolder { private static final ThreadLocal<

快速项目说明:我们有一个基于JSF2+Spring的动态数据源构建的应用程序。数据引用控件是通过spring配置创建的:

<bean id="dataSource" class="com.xxxx.xxxx.CustomerRoutingDataSource">
....
上面调用的CustomerContextHolder如下所示:

public class CustomerContextHolder {

private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

public static void setCustomerType(String customerType) {
    contextHolder.set(customerType);
}

public static String getCustomerType() {

    String manager = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dataBaseManager");

    if (manager != null) {
        contextHolder.set(manager);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("dataBaseManager", null);
    } else {
        String base =     (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("currentDatabBase");
        if (base != null)
            contextHolder.set(base);
    }
    return (String) contextHolder.get();
}

public static void clearCustomerType() {
    contextHolder.remove();
}
}
公共类CustomerContextHolder{
private static final ThreadLocal contextHolder=new ThreadLocal();
公共静态无效setCustomerType(字符串customerType){
contextHolder.set(customerType);
}
公共静态字符串getCustomerType(){
字符串管理器=(字符串)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(“数据库管理器”);
if(manager!=null){
contextHolder.set(管理器);
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(“dataBaseManager”,null);
}否则{
String base=(String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(“CurrentDatabase”);
if(base!=null)
contextHolder.set(基);
}
返回(字符串)contextHolder.get();
}
公共静态无效clearCustomerType(){
contextHolder.remove();
}
}
问题是最后一个人正在调用FacesContext.getCurrentInstance()来获取servlet上下文。只是解释一下,它使用会话属性dataBaseManager来告诉它应该使用哪个基。 对于实际的解决方案,它工作得很好,但是对于RESTEASY web服务的实现,当我们发出get请求时,FacesContext.getCurrentInstance()显然返回null并崩溃

我搜索了很多,但找不到从@GET params外部获取servlet上下文的方法。我想知道是否有任何方法可以得到它,或者是否有另一种解决我的动态数据源问题的方法


谢谢

就像魔法一样,可能没有多少人知道

我深入搜索了Resteasy文档,找到了Resteasy JAR附带的springmvc插件的一部分,它有一个名为RequestUtil.class的类。 有了它,我就能够在没有“@Context-HttpServletRequest-req”参数的情况下使用getRequest()方法

使用它,我能够在请求属性上设置所需的数据库,并从另一个线程(spring调用)获取它并从正确的位置加载内容

我现在用了一个星期,效果很好。我只需要将上面的determineLookupKey()更改为:

    @Override
protected String determineCurrentLookupKey() {
    if (FacesContext.getCurrentInstance() == null) {
        //RESTEASY
        HttpServletRequest hsr = RequestUtil.getRequest();
        String lookUpKey = (String) hsr.getAttribute("dataBaseManager");
        return lookUpKey;
    }else{
        //JSF
        return CustomerContextHolder.getCustomerType();         
    }
}
希望这能帮助其他人

蒂亚戈

    @Override
protected String determineCurrentLookupKey() {
    if (FacesContext.getCurrentInstance() == null) {
        //RESTEASY
        HttpServletRequest hsr = RequestUtil.getRequest();
        String lookUpKey = (String) hsr.getAttribute("dataBaseManager");
        return lookUpKey;
    }else{
        //JSF
        return CustomerContextHolder.getCustomerType();         
    }
}