axis2+spring集成

轉載自:http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html

一、新建一個web project項目,最終工程目錄以下:

注意:本文只注重webservice服務器端的開發,所以com.ljq.client和com.ljq.test忽略不計html

         

二、添加所需jarjava

精簡包web

            

三、接口HelloWorldspring

package com.ljq.service;

public interface HelloWorld {
public String greeting(String name);
public String print();
}

         

四、接口實現類HelloWorldBeanapache

複製代碼
package com.ljq.service;

public class HelloWorldBean implements HelloWorld {
public String greeting(String name) {
return "你好 "+name;
}

public String print() {
return "我叫林計欽";
}
}
複製代碼

          

五、webservice類HelloWorldWebServicetomcat

複製代碼
package com.ljq.service;

import org.apache.axis2.AxisFault;
import org.apache.axis2.ServiceObjectSupplier;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.i18n.Messages;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
* 可能出現Axis2 spring bean not found 或者 Spring applicationContext not found。
*
* 解決辦法:構建本身的ServiceObjectSupplier,實現接口ServiceObjectSupplier,同時也實現Spring的ApplicationContextAware接口
*
*
* @author Administrator
*
*/
public class HelloWorldWebService implements ServiceObjectSupplier,
ApplicationContextAware {
private static ApplicationContext ctx;

public Object getServiceObject(AxisService axisService) throws AxisFault {
Parameter springBeanName = axisService.getParameter("SpringBeanName");
String beanName = ((String) springBeanName.getValue()).trim();
if (beanName != null) {
if (ctx == null)
throw new AxisFault("applicationContext is NULL! ");
if (ctx.getBean(beanName) == null)
throw new AxisFault("Axis2 Can't find Spring Bean: " + beanName);
return ctx.getBean(beanName);
} else {
throw new AxisFault(Messages.getMessage("paramIsNotSpecified",
"SERVICE_SPRING_BEANNAME"));
}

}

public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
this.ctx = ctx;
}

}
複製代碼

            

六、配置web.xml文件服務器

複製代碼
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 添加spring監聽器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 加載spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>


<!-- 註冊axis2的servlet -->
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
複製代碼

       

七、在WEB-INF目錄下配置applicationContext.xml(不存在則本身建立)app

複製代碼
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="applicationContext"
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean id="helloWorld" class="com.ljq.service.HelloWorldBean"></bean>
</beans>
複製代碼

         

八、在WEB-INF\services\axis\META-INF\目錄下配置services.xml(不存在則本身建立)jsp

複製代碼
<?xml version="1.0" encoding="UTF-8"?>
<service name="hwWebService">
<description>axis2與spring集成案例</description>
<!-- 經過ServiceObjectSupplier參數指定SpringServletContextObjectSupplier類來得到Spring的ApplicationContext對象 -->
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
</parameter>
<!--
SpringBeanName固定的不能改
helloWorld是spring中註冊的實現類得id
-->
<parameter name="SpringBeanName">helloWorld</parameter>
<!--
在這裏最值得注意的是<messageReceivers>元素,該元素用於設置處理WebService方法的處理器。
例如,getGreeting方法有一個返回值,所以,須要使用可處理輸入輸出的RPCMessageReceiver類,
而update方法沒有返回值,所以,須要使用只能處理輸入的RPCInOnlyMessageReceiver類。
-->
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
複製代碼

        

axis2+spring集成到此已經開發完成,接下把工程部署到tomcat,post

而後經過http://localhost:8083/axis2spring/services/hwWebService?wsdl訪問

相關文章
相關標籤/搜索