Spring Integrated Hessian

Source: Internet
Author: User

Spring makes Hessian not only powerful, but also easy to use, but there are many pitfalls behind the ease of use! This is a simple example, but it actually took me more than an hour to rule out all sorts of problems and finally the problem finally came to the bottom. Integrate the above-mentioned Hello Hessian as a basis to join the spring framework for improvements. First, the environmentJdk1.5http://labs.xiaonei.com/apache-mirror/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.zip
http://s3.amazonaws.com/dist.springframework.org/release/SPR/ Spring-framework-2.5.6.sec01-with-dependencies.zip by the way, if you don't say the environment version, it's hard to guarantee that your program will work in another version. Second, integration1, write spring's release Hessian service configuration file hessian-servlet.xml<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >
<Beans>
<BeanID= "Defaulthandlermapping"class= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<BeanID= "HelloService"class= "Lavasoft.suths.service.HelloService"/>
<Beanname= "/hello"class= "Org.springframework.remoting.caucho.HessianServiceExporter">
< Property name= "Service"ref= "HelloService"/>
< Propertyname= "Serviceinterface"value= "Lavasoft.suths.service.Hello"/>
</Bean>
</Beans>2. Configure Web. xml<?XMLversion= "1.0"encoding= "UTF-8"?>
<Web-appxmlns= "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_2_5.xsd "
version= "2.5">
<Context-param>
<Param-name>Contextconfiglocation</Param-name>
<Param-value>
/web-inf/hessian-servlet.xml
</Param-value>
</Context-param>
<servlet>
<Servlet-name>Hessian</Servlet-name>
<Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>
<Load-on-startup>4</Load-on-startup>
</servlet>

<servlet-mapping>
<Servlet-name>Hessian</Servlet-name>
<Url-pattern>/hessian/*</Url-pattern>
</servlet-mapping>
</Web-app>Trap: a) The filename of the hessian-servlet.xml must start with the <servlet-name>hessian</servlet-name> name, and add a-servlet.xml section to make a complete file name. b) The file name format of the hessian-servlet.xml must be [servlet-name]-servlet.xml format, otherwise an error occurs. III. Deployment of ApplicationsBecause it involves class loading order problem, use idea to start Tomcat test old failure, do not know why! This time without idea embedded launcher tomcat, directly deploy the test yourself. After deployment, the startup interface is as follows: Third, write the testThis test, can be modified on the basis of the previous example to test, according to the above configuration, then the request HelloService URL should be: Http://localhost:8080/hessianapp/hessian/hello PackageLavasoft.suths.service.client;

ImportCom.caucho.hessian.client.HessianProxyFactory;
ImportLavasoft.suths.service.Hello;

ImportJava.net.MalformedURLException;

/**
* Client invoke (depends on service interface)
*
* @author leizhimin 2009-8-14 12:29:33
*/
PublicclassClient {
PublicStaticvoidMain (string[] args)throwsmalformedurlexception {
String URL ="http://localhost:8080/hessianapp/hessian/hello";
Hessianproxyfactory factory = new hessianproxyfactory ();
Hello hello = (hello) factory.create (hello). Class, URL);
System.out.println (Hello.sayhello ("Hessian"));
}
}Run Result: Hello hessian!

Process finished with exit code 0 There is also a test method, that is, the client also use spring, need to do a configuration remoting-client.xml:<! DOCTYPE Beans Public"-//spring//dtd bean//en "" Http://www.springframework.org/dtd/spring-beans.dtd ">
<beans>
<bean id="Helloserviceclient"class="Org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="Serviceurl"Value="http://localhost:8080/hessianapp/hessian/hello"/>
<property name="Serviceinterface" value="Lavasoft.suths.service.Hello"/>
</bean>
</beans>Then write a test class: PackageLavasoft.suths.service.client;

ImportLavasoft.suths.service.Hello;
ImportOrg.springframework.context.ApplicationContext;
ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Spring Integrated Hessian, client testing
*
* @author leizhimin 2009-8-14 15:32:46
*/
Publicclasstestclient {
PublicStaticvoidMain (string[] args) {
Try{
ApplicationContext context =NewClasspathxmlapplicationcontext ("/remoting-client.xml");
Hello hello = (hello) context.getbean ("Helloserviceclient");
System.out.println (Hello.sayhello ("Spring hession"));
}
Catch(Exception e) {
E.printstacktrace ();
}
}
Run Result: Hello Spring hession!

Process finished with exit Code 0 trap: In fact, look at the code well, the program sometimes can not run, because of the Hessian version of the problem, it is recommended to use the spring comes with the version, there will be no problem. The whole project so dependent on the package: Log4j-1.2.15.jar
Spring-aop.jar
Spring-beans.jar
Spring-context.jar
Spring-context-support.jar
Spring-core.jar
Spring-jdbc.jar
Spring-jms.jar
Spring-orm.jar
Spring-test.jar
Spring-tx.jar
Spring-web.jar
Spring-webmvc.jar
Spring-webmvc-portlet.jar
Spring-webmvc-struts.jar
Hessian-3.1.3.jar
Aopalliance.jar
Commons-logging.jar

Spring Integrated Hessian

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.