N久之后,找到了这个http://ws.apache.org/axis2/1_4_1/spring.html
官方网站上介绍两种方式整合,一种是我们所熟悉的applicationContext.xml放在和web.xml同一级的目录下,第二种是放在aar里面。
网上例子倒是一大把,蛋疼的是大多都是第二种,而且没涉及到数据库访问,也没提到spring中怎么配置
哎,官网上全是E问。幸好关于配置的还是看的点懂。我这里是选用第一种方式,而且我也只记录第一种,第二种涉及到spring注入我就不知道怎么搞了,
下面开始。
1、首先,需要axis2.war,根据它的目录结构我们修改自己的web应用。只需拷贝axis2/WEB-INF/目录下的所有文件到自己的web项目下就可以了,记得要覆盖所有(axis2-web这个文件夹是包含axis2整个的管理界面,可有可无,不过要用的话,记得它是放在和WEB-INF的同级目录下),其实我们就是有点像在整个的axis2工程上做二次开发。
WebRoot结构,如下图所示:
src结构,如图:
因为是小例子。所以就没那么规范了,webservice里放的是dao和entity以及.hbm.xml配置
2、我们像做所有其它的Web项目一样,用到spring的时候拷贝spring.jar到WEB-INF/lib目录下,然后在web.xml同级的目录下建立一个 applicationContext.xml,并且在applicationContext.xml引用相应的DTD或XSD(spring1.2引用的是DTD,spring2.0引用的是XSD),然后会在web.xml中加入这一段(当然,你可以选择用其它方式配置spring对web项目的支持)。
web.xml配置:
3、发布整个项目发部到tomcat上,测试起动服务有没有问题。起动没问题的话就访问下http://<host>:<port>/<project name>/services/Version?wsdl,只要成功就可以了,下面进入正题。
4、写一个接口。(spring面向接口编程)
package com.zero.service;
import java.util.List;
import com.zero.webservice.Test;
public interface ISpringwebservice {
//查询所有
public List<Test> getAll();
}
5、实现这个接口。
package com.zero.service.impl;
import java.util.List;
import com.zero.service.ISpringwebservice;
import com.zero.webservice.Test;
import com.zero.webservice.TestDAO;
public class SpringwebserviceImpl implements ISpringwebservice {
// spring注入
private TestDAO dao;
public void setDao(TestDAO dao) {
this.dao = dao;
}
// 查询所有
public List<Test> getAll() {
List<Test> list = dao.findAll();
return list;
}
}
6、写一个Web Service。
package com.zero.service.impl;
import java.util.List;
import com.zero.service.ISpringwebservice;
import com.zero.webservice.Test;
public class MyWebService {
// spring注入
private ISpringwebservice iservice;
public void setIservice(ISpringwebservice iservice) {
this.iservice = iservice;
}
// 查询所有
public Test[] getAll() {
Test[] arr = null;
List<Test> list = iservice.getAll();
if (!list.isEmpty()) {
// 貌似webservice不能直接使用List传值,要么用数组,要么用对象封装List
arr = new Test[list.size()];
for (int i = 0; i < list.size(); i++) {
arr[i] = list.get(i);
}
}
return arr;
}
}
src下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="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- basedao -->
<bean id="TestDAO" class="com.zero.webservice.TestDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- service -->
<bean id="SpringWebService" class="com.zero.service.impl.SpringwebserviceImpl" >
<property name="dao" ref="TestDAO"></property>
</bean>
</beans>
WEB-INF下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">
<import resource="classes/applicationContext.xml" /><!-- 导入src下的配置文件 相对路径 -->
<!-- 这个一定要有 -->
<bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"></bean>
<!-- MyWebservice -->
<bean id="myservice" class="com.zero.service.impl.MyWebService" >
<property name="iservice">
<ref bean="SpringWebService" /><!-- src下applicationContext.xml中的service -->
</property>
</bean>
</beans>
7、在WEB-INF/services/下新建一个文件夹,这个名字可以起的随便,我们就起做teswst吧,然后在WEB-INF/services/testws/下建立META-INF这个目录,最后在WEB-INF/services/testws/META-INF/下建立services.xml,文件
结构如图:
services.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<service name= "TestWebService" >
<description>列表查询</description>
<parameter name= "ServiceObjectSupplier" >
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter>
<parameter name= "SpringBeanName" >myservice</parameter> <!-- WEB-INF下applicationContext ben的id -->
<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>
OK.搞定,再发布启动服务,没问题,访问http://<host>:<port>/<project name>/services/TestWebService?wsdl看看
OK. 服务端搞定!
恩,因为axis2包比较多,所以源码就不上传了。
最近闲来无事,又做了一遍,效果比第一次的要好,2个方法都没问题,现在打包放上去,包含服务端和客户端以及sql语句
都放到百度云盘了