Caucho maker of Resin Server | Application Server (Java EE Certified) and Web Server


Resin Documentation

home company blog wiki docs 
app server 
 Resin Server | Application Server (Java EE Certified) and Web Server

hessian with dependency injection

Using Hessian with Dependency Injection pattern creates services which are simpler, protocol-independent and more easily tested. The Dependency Injection pattern (aka inversion of control) gives Resin the responsibility of configuring and assembling the service, protocol, and client.

Files in this tutorial

WEB-INF/classes/example/GreetingAPI.javaInterface for the greeting service.
WEB-INF/classes/example/GreetingImpl.javaThe service implementation as a Java object
WEB-INF/resin-web.xmlConfigures the environment
WEB-INF/classes/example/GreetingClientServlet.javaClient Servlet

Service Implementation
package example;

public interface GreetingAPI {
  public String hello();

Service Implementation

The Greeting implementation is a plain Java class that implements the MatchService API. Making the service a plain class offers a number of advantages:

  • Simplicity: It can concentrate on its business logic because it doesn't need to implement any protocol-dependent code.
  • Independence: It can be reused more easily because it isn't tied to a distributed framework (e.g. in contrast to EJB).
  • Testability: It is more easily tested since the test harness doesn't need to implement the protocol or its stubs. The harness can just test the service as a plain old Java object.
package example;

public class GreetingImpl implements GreetingAPI {
  private String _greeting = "Hello, world";

  public void setGreeting(String greeting)
    _greeting = greeting;

  public String greeting()
    return _greeting;

Configuring the Service

URL-based services can use the servlet configuration to define the service. The service class can use Resin IoC to inject its own dependencies.

<web-app xmlns="">

  <servlet-mapping url-pattern="/hessian/greeting"
    <protocol type="hessian"/>
      <greeting>Hello from resin-web.xml</greeting>



Configuring the client servlet with Dependency Injection allows for a simpler and more general client. The following client can use any proxy/stub which implements the GreetingAPI without change, for example:

  • Hessian proxy
  • Burlap proxy
  • EJB local object stub
  • JMX proxy
  • Java bean instance

Using the Dependency Injection pattern, the servlet doesn't care how the proxy is implemented, or how the greeting service is discovered.
import javax.webbeans.In;

public class GreetingClientServlet extends GenericServlet {
  @In private GreetingAPI _greeting;

  public void service(ServletRequest req, ServletResponse res)
    throws IOException, ServletException
    PrintWriter out = res.getWriter();

    out.println("Hello: " + _greeting.greeting());

Hessian Client using Dependency Injection

The following code defines the client in the resin.web.xml. The servlet defined above will inject the GreetingAPI directly with the WebBeans @In annotation. Because the GreetingAPI is unique, there's no need to give it a name.

<web-app xmlns="">

  <web-service-client class="example.GreetingAPI"

  <servlet-mapping url-pattern="/client/greeting"


Copyright © 1998-2012 Caucho Technology, Inc. All rights reserved. Resin ® is a registered trademark. Quercustm, and Hessiantm are trademarks of Caucho Technology.

Cloud-optimized Resin Server is a Java EE certified Java Application Server, and Web Server, and Distributed Cache Server (Memcached).
Leading companies worldwide with demand for reliability and high performance web applications including, CNET, DZone and many more are powered by Resin.

home company blog wiki docs 
app server