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.
package example; public interface GreetingAPI { public String hello(); } Service ImplementationThe Greeting implementation is a plain Java class that implements the MatchService API. Making the service a plain class offers a number of advantages:
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 ServiceURL-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="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin" xmlns:example="urn:java:example"> <example:GreetingImpl> <resin:Unbound/> <resin:HessianService urlPattern="/hessian/greeting"/> <greeting>Hello from resin-web.xml</greeting> </example:GreetingImpl> </web-app> 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:
Using the Dependency Injection pattern, the servlet doesn't care how the proxy is implemented, or how the greeting service is discovered. import javax.inject.Inject; public class GreetingClientServlet extends GenericServlet { @Inject 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 InjectionThe following code defines the client in the resin.web.xml.
The servlet defined above will inject the <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin" xmlns:example="urn:java:example"> <example:GreetingAPI> <resin:HessianClient url="http:${webApp.url}/hessian/greeting"/> </example:GreetingAPI> <servlet-mapping url-pattern="/client/greeting" servlet-class="example.GreetingClientServlet"/> </web-app>
|