在现今的
Web
应用中经常使用
Spring
框架来装载
JavaBean
。如果要想将某些在
中装配的
发布成
WebService
,使用
Axis2
的
感知功能是非常容易做到的。
在本文的例子中,除了 <Tomcat 安装目录 >\webapps\axis2 目录及该目录中的相关库外,还需要 框架中的 spring.jar 文件,将该文件复制到 >\webapps\axis2\WEB-INF\lib 目录中。
下面先建立一个 (该 最终要被发布成 ),代码如下:
在本文的例子中,除了 <Tomcat 安装目录 >\webapps\axis2 目录及该目录中的相关库外,还需要 框架中的 spring.jar 文件,将该文件复制到 >\webapps\axis2\WEB-INF\lib 目录中。
下面先建立一个 (该 最终要被发布成 ),代码如下:
package
service;
import entity.Person;
public class SpringService
{
private String name;
String job;
void setName(String name)
{
this .name = name;
}
setJob(String job)
{
.job job;
}
Person getPerson()
{
Person person new Person();
person.setName(name);
person.setJob(job);
return person;
}
String getGreeting(String name)
{
" hello + name;
}
}
import entity.Person;
public class SpringService
{
private String name;
String job;
void setName(String name)
{
this .name = name;
}
setJob(String job)
{
.job job;
}
Person getPerson()
{
Person person new Person();
person.setName(name);
person.setJob(job);
return person;
}
String getGreeting(String name)
{
" hello + name;
}
}
entity;
Person
{
String getName()
{
String getJob()
{
job;
}
}
Person
{
String getName()
{
String getJob()
{
job;
}
}
<
listener
>
listener-class org.springframework.web.context.ContextLoaderListener </
context-param
param-name contextConfigLocation param-value /WEB-INF/applicationContext.xml >
listener-class org.springframework.web.context.ContextLoaderListener </
context-param
param-name contextConfigLocation param-value /WEB-INF/applicationContext.xml >
在<Tomcat
安装目录>\webapps\axis2\WEB-INF
目录中建立一个applicationContext.xml
文件,该文件是Spring
框架用于装配JavaBean
的配置文件,内容如下:
<?
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:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation ="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
bean id ="springService" class ="service.SpringService"
property name ="name" value ="姚明" /> ="job" ="职业男篮"
bean
beans
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:tx ="http://www.springframework.org/schema/tx"
xsi:schemaLocation ="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
bean id ="springService" class ="service.SpringService"
property name ="name" value ="姚明" /> ="job" ="职业男篮"
bean
beans
在applicationContext.xml
文件中装配了service.SpringService
类,并被始化了name
和job
属性。在配置完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
文件,内容如下:
service
="springService"
description
Spring aware
parameter ="ServiceObjectsupplier"
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectsupplier
parameter ="SpringBeanName"
springService
messageReceivers messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class ="org.apache.axis2.rpc.receivers.RPcmessageReceiver" service
description
Spring aware
parameter ="ServiceObjectsupplier"
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectsupplier
parameter ="SpringBeanName"
springService
messageReceivers messageReceiver mep ="http://www.w3.org/2004/08/wsdl/in-out"
class ="org.apache.axis2.rpc.receivers.RPcmessageReceiver" 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 需要注意以下几点:
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
文件即可。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。