Chapter 25. Configuring Individual JAX-RS Resource Beans
When you scan your path for JAX-RS annotated resource beans, your beans are registered in per-request mode. This means that an instance will be created for every HTTP request served. You will usually require information from your environment. If you run a
WAR
in a Servlet container with Beta 2 or lower, you can only use JNDI lookups to obtain references to Java EE resources and configuration information. In this case, define your EE configuration (that is, ejb-ref
, env-entry
, persistence-context-ref
, etc.) in the web.xml
of your RESTEasy WAR
file. Then, within your code, perform JNDI lookups in the java:comp
namespace. For example:
web.xml
<ejb-ref> <ejb-ref-name>ejb/foo</ejb-ref-name> ... </ejb-ref>
Resource Code:
@Path("/") public class MyBean { public Object getSomethingFromJndi() { new InitialContext.lookup("java:comp/ejb/foo"); } ... }
You can also configure and register your beans manually through the Registry. In a
WAR
-based deployment, you must write a specific ServletContextListener
to do this. The listener lets you obtain a reference to the Registry, like so:
public class MyManualConfig implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { Registry registry = (Registry) event.getServletContext().getAttribute(Registry.class.getName()); } ... }
We recommend investigating Spring Integration and the Embedded Container's Spring Integration for a full understanding of this process.