Resin 4 CDI Dependency Injection Basic Example

From Resin 4.0 Wiki

Revision as of 00:00, 15 June 2012 by Rick (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

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.

Overview

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 [../../admin/candi.xtp Java Injection].

Some typical resources include:

  • Databases
  • JMS connections
  • EJB stateful and stateless session beans
  • JCA resources

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 TestResource bean

The TestResource bean is almost as simple as possible. It has a single String configuration parameter and does nothing but print its value using toString().

TestResource.java

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.


resin-web.xml configuration

The resin-web.xml (or resin.conf) configures the resource with the <test:TestResource> 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>.

resin-web.xml

<web-app xmlns="http://caucho.com/ns/resin">

  <test:TestResource xmlns:test="urn:java:test">
    <test:value>An Example Resource</test:value>
  </test:TestResource>

</web-app>
tag description
test:TestResource The classname of the resource bean
Named the optional Java Injection name of the resource for PHP
value The example bean's setValue parameter.


Using the resource from PHP

Your PHP scripts can use any Java Injection resource by using the java_bean() method. The method will return the named resource.

test.php

<php?

$resource = java_bean("testResource");

echo "PHP resource: " . $resource . "\n";

?>


Using the resource from JSP

JSP pages can also use WebBeans resource in the JSP expression language.

test.jsp


JSP resource: ${testResource}


Using the resource from a Servlet

The example uses a servlet to demonstrate the resource, but any injectable class could use @javax.inject.Inject to look up and use the resource. Because resources are stored globally in Java Injection, they can avoid the complexity of passing objects around or storing in the servlet context.

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.

TestServlet.java

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.inject.Inject;

public class TestServlet extends HttpServlet {
  @Inject private TestResource _resource;
  
  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Resource: " + _resource);
  }
}

results

Resource: TestResource[An example resource]
Personal tools
TOOLBOX
LANGUAGES