Rest 如何从webservice获取JSF应用程序范围的bean?

Rest 如何从webservice获取JSF应用程序范围的bean?,rest,jsf,applicationcontext,application-scope,Rest,Jsf,Applicationcontext,Application Scope,我有一个没有Spring的JSF2.0应用程序,我实现了一个缓存作为应用程序范围,应该从Rest服务访问它。现在我想调用RESTWebService,它应该检查缓存,但是当我想要访问它时,它总是空的 我已经试过了,这一个,但对我不起作用 @ManagedBean(eager=true) @ApplicationScoped public class CacheController implements Serializable{ private static final long se

我有一个没有Spring的JSF2.0应用程序,我实现了一个缓存作为应用程序范围,应该从Rest服务访问它。现在我想调用RESTWebService,它应该检查缓存,但是当我想要访问它时,它总是空的

我已经试过了,这一个,但对我不起作用

@ManagedBean(eager=true)
@ApplicationScoped
public class CacheController implements Serializable{

    private static final long serialVersionUID = 123L;

    private Map<String, Cache> map = new HashMap<String, Cache>();

    public Map<String, Cache> getMap() {
        return map;
    }

    public void setMap(Map<String, Cache> map) {
        this.map = map;
    }

}


@Path("/service")
public class RestService {

    @POST
    @Path("anlieferung/kennzahlen")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getValueFromCache(String item) throws JSONException, ParseException {

                //is always null
        CacheController cacheController= (CacheController) getServletContext().getAttribute("cacheController");

                //is always null
        FacesContext context = FacesContext.getCurrentInstance();
            Application application = context.getApplication();
            CacheController cacheBean = application.evaluateExpressionGet(context, "#{cacheController}", CacheController.class);

        //doSomeStuff and check if the item is in the Cache (CacheController.getMap())
        }
}
@ManagedBean(eager=true)
@适用范围
公共类CacheController实现可序列化{
私有静态最终长serialVersionUID=123L;
私有映射映射=新的HashMap();
公共地图getMap(){
返回图;
}
公共无效集合映射(映射映射){
this.map=map;
}
}
@路径(“/服务”)
公共类服务{
@职位
@路径(“安利费隆/肯扎伦”)
@产生(MediaType.APPLICATION_JSON)
@使用(MediaType.APPLICATION_JSON)
公共字符串getValueFromCache(字符串项)抛出JSONException,ParseException{
//始终为空
CacheController CacheController=(CacheController)getServletContext().getAttribute(“CacheController”);
//始终为空
FacesContext context=FacesContext.getCurrentInstance();
Application Application=context.getApplication();
CacheController cacheBean=application.evaluateExpressionGet(上下文“#{CacheController}”,CacheController.class);
//doSomeStuff并检查该项是否在缓存中(CacheController.getMap())
}
}

我在jsf应用程序上初始化过缓存,它可以正常工作。现在,我希望通过FacesContent或ServletContext获取缓存对象,但它总是空的。我需要创建类似ServletListener的东西吗?谁能给我举个例子吗?谢谢

有帮助吗?实际上,您需要在rest代码中使用servletContext。Hmmm的可能副本也是其他内容的副本:我现在明白了,我无法获得FacesContext,因为我不在“JSF世界”。我不明白当会话被破坏时,为什么要在httpSessionListener中实现一些东西。此外,我还尝试了CacheController bean=(CacheController)getServletContext().getAttribute(“CacheController”);但是它也总是空的。您的REST端点不是CDI管理的bean有什么原因吗?您可以将它设置为
@requestscope
(由于其无状态),然后只需
@将缓存注入其中。