对于EJB将被取代的说法已经比比皆是了,虽然我不认同吧,但是实话实说对于大多数的项目来说使用企业级的java bean还真的是没什么必要,因为太重量级了,上篇博客不是说了一下简单的hessian吗,这里就来说说hessian和spring的结合。
1、引入jar包
需要的jar包有:
2、配置remote-servlet.xml文件(在src目录下)
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 接口的具体实现类 --> <bean id="impl" class="com.tgb.hessian.service.impl.HelloServiceImpl" /> <!-- 使用Spring的HessianServie做代理 --> <bean name="/helloSpring" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- service引用具体的实现实体Bean--> <property name="service" ref="impl" /> <property name="serviceInterface" value="com.tgb.hessian.service.HelloServiceSpring" /> </bean> <!-- 可以配置多个HessianServiceExporter代理Bean --> </beans>
3、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Spring-Hessian</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>remote</servlet-name> <!-- 使用Spring的代理Servlet --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>classes/remote-servlet</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remote/*</url-pattern> </servlet-mapping> </web-app>
4、服务接口和 接口的实现
(1)接口
package com.tgb.hessian.service; public interface HelloServiceSpring { /** * hellworld 服务类接口 * @author 陈丽娜 * @version 2015年7月26日下午6:18:41 * @return */ public String HellowWorld(String name); }
(2)实现
package com.tgb.hessian.service.impl; import com.tgb.hessian.service.HelloServiceSpring; /** * 服务实现类 * @author 陈丽娜 * @version 2015年7月26日下午9:17:03 */ public class HelloServiceImpl implements HelloServiceSpring { @Override public String HellowWorld(String name) { // TODO Auto-generated method stub return "hello" + name; } }
5、客户端的相关配置(同样需要引入spring的jar包和hessian的jar包,还有服务接口的jar。这里就不再赘述)
remote-client.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 客户端Hessian代理工厂Bean --> <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <!-- 请求代理Servlet路径 --> <property name="serviceUrl"> <value>http://localhost:8080/Spring-Hessian/remote/helloSpring</value> </property> <!-- 接口定义 --> <property name="serviceInterface"> <value>com.tgb.hessian.service.HelloServiceSpring</value> </property> </bean> </beans>
6、测试类的编写
package com.tgb.hessian.test; import java.net.MalformedURLException; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tgb.hessian.service.HelloServiceSpring; public class TestHessianSpring { @Test public void Test1() throws MalformedURLException { ApplicationContext contex = new ClassPathXmlApplicationContext( "remote-client.xml"); // 获得客户端的Hessian代理工厂bean HelloServiceSpring i = (HelloServiceSpring) contex.getBean("clientSpring"); System.out.println(i.HellowWorld("陈丽娜")); } }
7、运行结果:
是不是很简洁呢?!赶快学起来吧!
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-11 01:30:10