加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebS

发布时间:2020-12-17 02:12:00 所属栏目:安全 来源:网络整理
导读:本文为原创,如需转载,请注明作者和出处,谢谢! ?? 在现今的 Web 应用中经常使用 Spring 框架来装载 JavaBean 。如果要想将某些在 Spring 中装配的 JavaBean 发布成 WebService ,使用 Axis2 的 Spring 感知功能是非常容易做到的。 ??? 在本文的例子中,除

本文为原创,如需转载,请注明作者和出处,谢谢!

?? 在现今的Web应用中经常使用Spring框架来装载JavaBean。如果要想将某些在Spring中装配的JavaBean发布成WebService,使用Axis2Spring感知功能是非常容易做到的。
??? 在本文的例子中,除了<Tomcat安装目录>/webapps/axis2目录及该目录中的相关库外,还需要Spring框架中的spring.jar文件,将该文件复制到<Tomcat安装目录>/webapps/axis2/WEB-INF/lib目录中。
??? 下面先建立一个JavaBean(该JavaBean最终要被发布成WebService),代码如下:

Code:
  1. package?service;??
  2. import?entity.Person;??
  3. public?class?SpringService??
  4. {??
  5. ????private?String?name;??
  6. ????private?String?job;??
  7. ????public?void?setName(String?name)??
  8. ????{??
  9. ????????this.name?=?name;??
  10. ????}??
  11. ????public?void?setJob(String?job)??
  12. ????{??
  13. ????????this.job?=?job;??
  14. ????}??
  15. ????public?Person?getPerson()??
  16. ????{??
  17. ????????Person?person?=?new?Person();??
  18. ????????person.setName(name);??
  19. ????????person.setJob(job);??
  20. ????????return?person;??
  21. ????}??
  22. ????public?String?getGreeting(String?name)??
  23. ????{??
  24. ????????return?"hello?"?+?name;??
  25. ????}??
  26. }??

?

??? 其中Person也是一个JavaBean,代码如下:

Code:
  1. package?entity;??
  2. public?class?Person??
  3. {??
  4. ????private?String?name;??
  5. ????private?String?job;??
  6. ????public?String?getName()??
  7. ????{??
  8. ????????return?name;??
  9. ????}??
  10. ????public?void?setName(String?name)??
  11. ????{??
  12. ????????this.name?=?name;??
  13. ????}??
  14. ????public?String?getJob()??
  15. ????{??
  16. ????????return?job;??
  17. ????}??
  18. ????public?void?setJob(String?job)??
  19. ????{??
  20. ????????this.job?=?job;??
  21. ????}??
  22. }?

?

??? 将上面两个Java源文件编译后,放到<Tomcat安装目录>/webapps/axis2/WEB-INF/classes目录中。

??? 在<Tomcat安装目录>/webapps/axis2/WEB-INF/web.xml文件中加入下面的内容:

Code:
  1. <listener>??
  2. ???? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>??
  3. </listener>??
  4. <context-param>??
  5. ???? <param-name>contextConfigLocation</param-name>??
  6. ???? <param-value>/WEB-INF/applicationContext.xml</param-value>??
  7. </context-param>??

??? <Tomcat安装目录>/webapps/axis2/WEB-INF目录中建立一个applicationContext.xml文件,该文件是Spring框架用于装配JavaBean的配置文件,内容如下:

Code:
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ????????xmlns:aop="http://www.springframework.org/schema/aop"??
  5. ????????xmlns:tx="http://www.springframework.org/schema/tx"??
  6. ????????xsi:schemaLocation="??
  7. ????????????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.5.xsd??
  8. ????????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.5.xsd??
  9. ????????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">??
  10. ??<bean?id="springService"?class="service.SpringService">??
  11. ?????<property?name="name"?value="姚明"?/>??
  12. ?????<property?name="job"?value="职业男篮"?/>???
  13. ??</bean>?????
  14. </beans>??

?

??? applicationContext.xml文件中装配了service.SpringService类,并被始化了namejob属性。在配置完SpringService类后,就可以直接在程序中FileSystemXmlApplicationContext类或其他类似功能的类读取applicationContext.xml文件中的内容,并获得SpringService类的对象实例。但现在我们并不这样做,而是将SpringService类发布成WebService

??? <Tomcat安装目录>/webapps/axis2/WEB-INF/lib目录中有一个axis2-spring-1.4.1.jar文件,该文件用于将被装配JavaBean的发布成WebService。在D盘建立一个axi2-spring-ws目录,并在该目录中建立一个META-INF子目录。在META-INF目录中建立一个services.xml文件,内容如下:

Code:
  1. <service?name="springService">??
  2. ????<description>??
  3. ????????Spring?aware??
  4. ????</description>??
  5. ????<parameter?name="ServiceObjectSupplier">??
  6. ????????org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier??
  7. ????</parameter>??
  8. ????<parameter?name="SpringBeanName">??
  9. ????????springService??
  10. ????</parameter>??
  11. ????<messageReceivers>??
  12. ????????<messageReceiver?mep="http://www.w3.org/2004/08/wsdl/in-out"??
  13. ????????????class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"?/>??
  14. ????</messageReceivers>??
  15. </service>???????

?

??? Windows控制台进入axi2-spring-ws目录,并使用jar命令将axi2-spring-ws目录中的内容打包成axi2-spring-ws.aar,然后将该文件复制到<Tomcat安装目录>/webapps/axis2/WEB-INF/services目录中,启动Tomcat后,就可以访问该WebService了,访问方式与前面几篇文章的访问方式相同。获得wsdl内容的URL如下:

http://localhost:8080/axis2/services/springService?wsdl

??? 在将Spring中的装配JavaBean发布成WebService需要注意以下几点:

??? 1.?JavaBean编译生成的.class文件需要放在WEB-INF/classes目录中,或打成.jar包后放在WEB-INF/lib目录中,而WEB-INF/services目录中的.aar包中不需要包含.class文件,而只需要包含一个META-INF目录,并在该目录中包含一个services.xml文件即可。

??? 2.?services.xml的配置方法与前几篇文章的配置方法类似,只是并不需要使用ServiceClass参数指定要发布成WebServiceJava类,而是要指定在applicationContext.xml文件中的装配JavaBean的名称(SpringBeanName参数)。

??? 3.?

services.xml

文件中需要通过

ServiceObjectSupplier

参数指定

SpringServletContextObjectSupplier

类来获得

Spring

ApplicationContext

对象。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读