hsf服务的调用过程

目录:

写一个hsf服务并发布

       写一个接口工程

       写一个实现接口的实现工程

       写一个发布接口工程和实现工程的服务工程

       写一个应用实现借口

hsf服务调用的过程

       通过JBoss/Tomcat启动服务

       通过main口启动服务

  


过程:

写一个hsf服务并发布:

        在上一篇博客中,模块(module)方式创建maven工程,已经建好了工程

        写一个接口工程lonkun-api中写入一个接口类

hsf服务的调用过程_第1张图片

接口类中只写被别人引用的pojo和接口这里定义了这两个接口,里面的内容想怎样写,自己可以决定的。

使用maven的install可以打成一个jar包到本地仓库中。

        写一个是实现类:

hsf服务的调用过程_第2张图片

实现类自己是实现,当然在pom.xml中要引入上一个jar包,不然实现时,找不到类啊!使用maven的install可以打成一个jar包到本地仓库中。

             写一个服务类:

hsf服务的调用过程_第3张图片

最主要的两个配置文件,web.xml中增加一个spring监听:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext.xml</param-value>
  </context-param>
  <listener>    
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

在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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean id="loginService" class="com.test.service.LoginService" />

  <bean class="com.taobao.hsf.app.spring.util.HSFSpringProviderBean" init-method="init">
    <property name="serviceInterface">
      <value>com.test.service.ILogin</value>
    </property>
    <property name="target">
      <ref bean="loginService" />
    </property>
    <property name="serviceVersion">
  <!-- 服务如果服务是发布在自己的开发机器上,请不要使用1.0.0.daily的版本号 -->
      <value>1.0.0.longkun.wyb</value>
    </property>
  </bean>
</beans>

上面的红色字提示说明是HSF的提供者,当解析这个配置文件的时候,服务就会启动起来。什么时候解析这个配置文件呢?后面讲解

使用maven,对整个工程进行maven package

找到longkun-sever.war(默认有这个文件,如果在配置文件中改变了名字就不是它了)。把longkun-server.war放到jboss的/server/default/deploy/下

启动JBoss,就可以启动服务了。启动的JBoss一定是阿里巴巴提供的改过的JBoss还有阿里巴巴提供的插件。服务启动了,应用就可以使用了。

                   使用时,通过spring注入,其中的实现工程就是一个代理而已,使用时要注意aplicationContext.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="loginService" class="com.taobao.hsf.app.spring.util.HSFSpringConsumerBean" init-method="init">
      <property name="interfaceName">
        <value>com.test.service.ILogin</value>
      </property>
      <property name="version">
        <value>1.0.0.longkun.wyb</value>
      </property>
    </bean>
</beans>

Java中代码是:

main(){

 HSFEasyStarter.startFromPath("C:\\jboss-4.2.2.GA\\server\\default\\deploy");
                Thread.sleep(1000);
            
                
//                new ClassPathXmlApplicationContext("ApplicationContext.xml");
                ApplicationContext context =  new ClassPathXmlApplicationContext("ApplicationContext.xml");
                System.out.println("Start......." +"...End!");
//                
                System.out.println("zhang ge z world");

ILogin loginService = (ILogin)context.getBean("loginService");
                User user = new User();
                user.setId(100);
                user.setName("zhang yao");
                user.setPassword("1234567890qqqqq");
                user.setDate(new Date());
                System.out.println("loginService....");
                if(loginService == null){
                    System.out.println("null.....");
                    return;
                }
//                System.out.println(loginService.check("1222"));
                System.out.println(loginService.getName(user));
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

hsf服务调用的过程:

          不管哪种方式启动,都需要解析ApplicationContext.xml这个配置文件

            JBoss启动方式是:JBoss服务器启动,然后是解析一些插件,这些插件(plugin)的具体作用,这里先不详讲,主要是启动阿里巴巴集团的一些必要的应用服务,

这里用到的是为后面发布服务提供环境。JBoss服务器启动,就要读取deploy目录下的web程序(带有web.xml文件的.war包),先解析web.xml文件,发现web.xml中

有spring容器的监听,那么解析ApplicationContext.xml文件并初始化,那么就把longkun-server服务启动起来了。

         main口启动(上面程序就是main口启动的):通过 HSFEasyStarter.startFromPath("C:\\jboss-4.2.2.GA\\server\\default\\deploy");分析插件,提供环境;

                                            ApplicationContext context =  new ClassPathXmlApplicationContext("ApplicationContext.xml");解析这个配置文件并初始化;

                                       服务启动了。

在web程序的应用端的ApplicationContext.xml中下面代码是必须的:

        <bean id="loginService" class="com.taobao.hsf.app.spring.util.HSFSpringConsumerBean" init-method="init">
      <property name="interfaceName">
        <value>com.test.service.ILogin</value>
      </property>
      <property name="version">
        <value>1.0.0.longkun.wyb</value>
      </property>
    </bean>

你可能感兴趣的:(spring,maven,bean,jboss,阿里巴巴,encoding)