documentation examples changes amber (jpa) ejb database ioc jmx jsf messaging quercus remoting servlet security basic resource injection periodic task ioc appconfig | a simple resource bean
Resources are beans configured in the resin.conf or resin-web.xml and stored in the WebBeans registry. The tutorial shows the configuration of a trivial bean as a resource and using it from a JSP file. A resource in Resin is any bean configured in the resin.conf or resin-web.xml. Resources are stored in the WebBeans registry. Because resources can be any Java class conforming to the bean configuration patterns, resources provide a flexible configuration. There is also more documentation for Resin's IoC/Dependency Injection. Some typical resources include:
Because resources are configured in the resin.conf/resin-web.xml and are created before any servlets start, they are very convenient for globally available beans, even allowing for more complex configuration beans extending the idea of <env-entry> (which stored simple Strings or Integers in JNDI.) The package test; public class TestResource { private String _value = "default"; public void setValue(String value) { _value = value; } public String toString() { return "TestResource[" + _value + "]"; } } In other words, you have lots of flexibility of things to configure as resources. The resin-web.xml (or resin.conf) configures the resource with the <bean> tag. The resource is created and stored in the environment where it is defined. So a resource configured in the <host> will be available for all web-apps in that host and a resource configured in a <web-app> will only be available for that <web-app>. <web-app xmlns="http://caucho.com/ns/resin"> <bean class="test.TestResource"> <init> <value>An Example Resource</value> </init> </bean> </web-app>
Your PHP scripts can use any WebBeans resource by using the
<php? $resource = java_bean("testResource"); echo "PHP resource: " . $resource . "\n"; ?> JSP pages can also use WebBeans resource in the JSP expression language. JSP resource: ${testResource} The example uses a servlet to demonstrate the resource, but any
injectable class could use If you save the resource, it's important that the saved field is reloaded when the web-app restarts. The resource has the same lifetime as its environment: web-app, host or cluster. When that environment closes, the resource is no longer valid and must be discarded. In particular, it is important to store any resource in an object's field, not in a static field or static hashtable. package test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.webbeans.In; public class TestServlet extends HttpServlet { @In private TestResource _resource; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); out.println("Resource: " + _resource); } } Resource: TestResource[An example resource]
|