我的第一个Hessian服务
开了账号这么多年了,今天第一次来这写东西。我今天要说说的写的第一个hessian服务。
这次我用的是spring+hessian来做的web服务。
首先建立这样一个服务要引入srping的一些包,主要有core,web,remoting还有一个重要的包叫aop。
就是因为没有这个包让的原本写好的程序莫名其妙的报错,找了半天才找到的。
首先发布服务我们要对外做一个接口:下面是代码
public interface IHelloHessian {
public String SayHello();
}
有了接口你总得通过接口做一些事吧,下面我们来做事
public interface IHelloHessian {
public String SayHello();
}
事是做完了。可是外部还是不能调用啊,spring要做的大量的工作就是配置,接下来我们来置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloHessian" class="com.kane.HelloHessian">
</bean>
</beans>
这完了以后,你要向外中指定用那个吧,那就再来一个对外用的XML吧。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 业务类 -->
<bean id="hessianService" class="com.kane.HelloHessian"/>
<!-- 远程服务 -->
<bean name="/helloHessian" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="helloHessian"/>
<property name="serviceInterface">
<value>
com.kane.IHelloHessian
</value>
</property>
</bean>
</beans>
这好了,spring + hessian是通过servlet拦截实现的,那现在我们就要配web.xml文件了。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>Hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
</web-app>
到现在为止你可以项目放到你的服务器上进行发布了。
我的文章到也就写完了。希望大家不要走弯路了。