Spring remoting quickstart

I have spent a lot of time on configuring Spring remoting properly (mostly because of problems with DTD).
To expose business logic via for example XFire Web Services we have to define context and listeners in web.xml, example:

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE web-app
PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”

http://java.sun.com/dtd/web-app_2_3.dtd”&gt;
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>

Configuration of beans that are going to be exposed via WS is stored in separate file, in this case in applicationContext.xml. Example of the content of the file:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans-2.0.dtd”&gt;
<beans>
<bean class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>
<property name=”urlMap”>
<map>
<entry key=”/WorkflowRegister”>
<ref bean=”workflowRegister”/>
</entry>
</map>
</property>
</bean>
<bean id=”workflowRegister” class=”org.codehaus.xfire.spring.remoting.XFireExporter”>
<property name=”serviceFactory”>
<ref bean=”xfire.serviceFactory”/>
</property>
<property name=”xfire”>
<ref bean=”xfire”/>
</property>
<property name=”serviceBean”>
<ref bean=”worklfowRegisterBean”/>
</property>
<property name=”serviceClass”>
<value>pl.edu.agh.gemini.adds.core.IWorkflowRegister</value>
</property>
</bean>
<bean id=”worklfowRegisterBean” class=”pl.edu.agh.gemini.adds.core.WorkflowRegister” factory-method=”getInstance” />
<import resource=”classpath:org/codehaus/xfire/spring/xfire.xml”/>
</beans>

As you can see defining new WS which is based on already implemented bean requires just few lines of XML. Business logic is totally separated from the way of exposing it. No changes are required in logic to expose it as Web Service! Also changing transport layer from XFire to e.g. Burlap/Hessian/RMI is just a matter of configuration in those two files.

Dependencies for Spring remoting (part of Maven pom.xml):

<dependencies>
<dependency>
<groupId>pl.edu.agh.gemini.adss</groupId>
<artifactId>adsscore</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>pl.edu.agh.gemini.adss</groupId>
<artifactId>adsscoreapi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-remoting</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-jaxb2</artifactId>
<version>1.2-RC</version>
</dependency>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-spring</artifactId>
<version>1.2-RC</version>
</dependency>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-java5</artifactId>
<version>1.2-RC</version>
</dependency>
</dependencies>

Application can be simply deployed as war file in servlet container (e.g. Tomcat), no J2EE application server is required.

Have fun with Spring remoting!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s