下载axis2
axis2-1.6.2-war.zip
把axis2.war放到tomcat的webapps目录下,启动tomcat,输入http://localhost:9080/axis2 出现后台页面即可。
结合spring发布axis2
一、HelloSpring
package service;
public class HelloSpring {
public String sayHello(String name){
return "你好,"+name;
}
public String sayGoodMorning(String name){
return "早上好,"+name;
}
}
新建一个目录test,进入目录后,新建META-INF目录,然后在里面新建services.xml
内容如下
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="helloSpring">
<description>
Spring aware
</description>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<parameter name="SpringBeanName">
spring-hello
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
</serviceGroup>
进入test目录,直接运行jar -cvf spring-hello.aar . (注意后面的点)
把生成的aar,复制到%Tomcat%/webapps/axis2/WEB-INF/services目录下。
把HelloSpring.class文件复制到%Tomcat%/webapps/axis2/WEB-INF/classes/service目录中
axis2项目的web.xml,增加
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
与web.xml同目录新建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"
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="spring-hello" class="service.HelloSpring"></bean>
</beans>
重启tomcat即可。输入http://localhost:9080/axis2 , 击services进入服务列表。
说明:例如spring打包不需要把.class文件打包。
1. 由JavaBean编译生成的.class文件需要放在WEB-INF\classes目录中,或打成.jar包后放在WEB-INF\lib目录中,而WEB-INF\services目录中的.aar包中不需要包含.class文件,而只需要包含一个META-INF目录,并在该目录中包含一个services.xml文件即可。
2. services.xml的配置方法与前几篇文章的配置方法类似,只是并不需要使用ServiceClass参数指定要发布成WebService的Java类,而是要指定在applicationContext.xml文件中的装配JavaBean的名称(SpringBeanName参数)。
3. 在services.xml文件中需要通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象。