Java 在JAX-RS请求之间共享变量
我有一个关于JAX-RS的非常基本的问题,但不知怎么的,我很难找到答案 我正在尝试将一个使用“标准”JavaXServlet(手工将请求路由到方法)的REST服务重构为一个“更干净”的JAX-RS实现。当前应用程序在servlet init()期间设置一些变量。它将这些属性指定为HttpServlet类的属性,以便它们在每个doGet()期间都可用,并且可以作为参数传递给请求处理方法。为清楚起见,其中一个是充当缓存的ConcurrenthashMap 现在,使用JAX-RS,我可以扩展应用程序来设置我的资源类。在处理请求时,我还可以在每个资源实现中使用@Context注释来注入诸如ServletContext之类的内容。但我不知道如何类似地注入应用程序初始化期间设置的变量 我使用的是JAX-RS的Apache Wink 1.3.0实现。在web应用程序启动之前,您可以对缓存使用init并将其设置为context as属性。如下所示:Java 在JAX-RS请求之间共享变量,java,jax-rs,apache-wink,Java,Jax Rs,Apache Wink,我有一个关于JAX-RS的非常基本的问题,但不知怎么的,我很难找到答案 我正在尝试将一个使用“标准”JavaXServlet(手工将请求路由到方法)的REST服务重构为一个“更干净”的JAX-RS实现。当前应用程序在servlet init()期间设置一些变量。它将这些属性指定为HttpServlet类的属性,以便它们在每个doGet()期间都可用,并且可以作为参数传递给请求处理方法。为清楚起见,其中一个是充当缓存的ConcurrenthashMap 现在,使用JAX-RS,我可以扩展应用程序来
package org.paulvargas.shared;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class CacheListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
Map<String, String> dummyCache = new HashMap<String, String>();
dummyCache.put("greeting", "Hello Word!");
ServletContext context = sce.getServletContext();
context.setAttribute("dummyCache", dummyCache);
}
public void contextDestroyed(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
context.removeAttribute("dummyCache");
}
}
您可以使用@Context
注释来插入ServletContext
并检索属性
package org.apache.wink.example.helloworld;
import java.util.*;
import javax.servlet.ServletContext;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import org.apache.wink.common.model.synd.*;
@Path("/world")
public class HelloWorld {
@Context
private ServletContext context;
public static final String ID = "helloworld:1";
@GET
@Produces(MediaType.APPLICATION_ATOM_XML)
public SyndEntry getGreeting() {
Map<String, String> dummyCache =
(Map<String, String>) context.getAttribute("dummyCache");
String text = dummyCache.get("greeting");
SyndEntry synd = new SyndEntry(new SyndText(text), ID, new Date());
return synd;
}
}
package org.apache.wink.example.helloworld;
导入java.util.*;
导入javax.servlet.ServletContext;
导入javax.ws.rs.*;
导入javax.ws.rs.core.*;
导入org.apache.wink.common.model.synd.*;
@路径(“/世界”)
公共类HelloWorld{
@上下文
私有服务上下文;
公共静态最终字符串ID=“helloworld:1”;
@得到
@产生(MediaType.APPLICATION\u ATOM\u XML)
公共助理{
映射dummyCache=
(Map)context.getAttribute(“dummyCache”);
String text=dummyCache.get(“问候”);
SyndEntry synd=new SyndEntry(new SyndText(text),ID,new Date());
返回synd;
}
}
您正在使用Spring吗?不,我没有使用Spring。这与我的想法很接近:在初始化期间设置servlet属性,并在每个请求期间从servlet上下文中获取它。但doFilter不会在每个请求期间运行。有没有一种方法可以在servlet的init()期间访问上下文并设置一次属性?看起来不错。谢谢你的帮助@保尔瓦加斯。。我在寻找类似的东西,但我希望在servletContext中启动一个executorservice,即初始化。非常感谢。对于您的答案:)嗨,@Dc01\u xx另一个解决方案是使用@Singleton
(企业JavaBean)。如果我有时间,我会把它添加到我的答案中。:-)
package org.apache.wink.example.helloworld;
import java.util.*;
import javax.servlet.ServletContext;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import org.apache.wink.common.model.synd.*;
@Path("/world")
public class HelloWorld {
@Context
private ServletContext context;
public static final String ID = "helloworld:1";
@GET
@Produces(MediaType.APPLICATION_ATOM_XML)
public SyndEntry getGreeting() {
Map<String, String> dummyCache =
(Map<String, String>) context.getAttribute("dummyCache");
String text = dummyCache.get("greeting");
SyndEntry synd = new SyndEntry(new SyndText(text), ID, new Date());
return synd;
}
}