HSF入门例子

1.搭建HSF环境

        实际上就是下载hsf.sar,然后放置到tomcat的应用目录下即可


2.服务端

        (1)创建web项目

        (2)添加编译和打包的插件


      1.6


      HSFSampleLanshan
      
           
               maven-compiler-plugin
               
                   ${java.version}
                   ${java.version}
               
          
     
        (3)添加spring和servlet依赖

	
		org.apache.geronimo.specs
		geronimo-servlet_3.0_spec
		1.0
		jar
		compile
	
	
		org.springframework
		spring
		2.5.6
		jar
		compile
	

        (4)创建一个HelloWorld服务,作为服务端,需要提供接口和实现

                  接口:

package com.hsf;

public interface HelloWorldService 
{
	public String sayHello(String name);
}
                 实现:

package com.hsf.impl;

import com.hsf.HelloWorldService;

public class HelloWorldServiceImpl implements HelloWorldService
{
	public String sayHello(String name) {
		return "hello "+name;
	}

}

        (5)为HelloWorld服务编写spring配置文件


  


    
	
		
			com.hsf.HelloWorldService
		
		
			
		
		
			1.0.0.cpf
		
		
			HSF
		
		
			HelloWorld
		
	

        (6)在web.xml中添加一个spring监听器,让容器启动的时候spring完成服务初始化
	
		org.springframework.web.context.ContextLoaderListener
	
                这里不配置applicationContext.xml的路径,默认位于WEB-INF下


         完成之后,打成war包并放在tomcat下,启动tomcat,即可完成服务的发布


3.客户端

        (1)创建web项目

                  这里,将server和client放在同一个项目中

        (2)创建服务的接口

                  以为本例使用的是同一个项目,所以就不编写了


        (3)编写一个类用于访问提供的服务(这里用servlet)

package com.controller;

import com.hsf.HelloWorldService;

public class HelloWorldServlet extends HttpServlet 
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		WebApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		HelloWorldService helloWorldService = (HelloWorldService) context
				.getBean("HelloWorldConsumer");
		PrintWriter out = resp.getWriter();
		out.println(helloWorldService.sayHello("lanshan"));
		return;
	}
}

        (4)编写客户端的spring配置文件

                  这里使用的是同一个项目,所以直接添加就好

   
	
		com.hsf.HelloWorldService
	
	
		1.0.0.cpf
	
    


        (5)web.xml中添加servlet的映射

	
		HelloWorldServlet
		com.controller.HelloWorldServlet
	
	
		HelloWorldServlet
		/HelloWorld
	

4.运行

        打包运行即可


你可能感兴趣的:(中间件)