日期:2014-05-16  浏览次数:20466 次

cxf应用--通过接口将数据保存到数据库
服务器端代码:
ITestSendData.java
package testcxf;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import pojo.TestJmsCxf;

@WebService
@SOAPBinding(style=Style.RPC)
public interface ITestSendData {
	public String getString(String str);
	
	public void testAddData(TestJmsCxf tjc);
}

package testcxf;

import javax.annotation.Resource;

import dao.TestJmsCxfDao;

import pojo.TestJmsCxf;

public class TestSendDataImpl implements ITestSendData {
	private TestJmsCxfDao testjcDao=new TestJmsCxfDao();
	
	public String getString(String str) {
		System.out.println("接收到的数据为: "+str);
		return "接收到的数据为: "+str;
	}

	public void testAddData(TestJmsCxf tjc) {
		testjcDao.insert(tjc);
	}
	
	public TestJmsCxfDao getTestjcDao() {
		return testjcDao;
	}
        //下面的注释一定要加上,否则spring无法注入,将数据保存到数据库的时候会造成空指针,此处我也不明白为什么要这么做,我觉得spring配置文件中已经注入了,而且set不也是一种注入的方式吗,但是不加这个的话就会报错,最后就试了这种注入的方法,成功了
	@Resource(name="TestJmsCxfDao")
	public void setTestjcDao(TestJmsCxfDao testjcDao) {
		this.testjcDao = testjcDao;
	}

}



applicationContext-server.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:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<bean id="sendDataServiceBean" class="testcxf.TestSendDataImpl" />

	<bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor">
	</bean>

	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
	<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
	<jaxws:server id="sendDataService" serviceClass="testcxf.ITestSendData" address="/SendData">
		<jaxws:serviceBean>
			<ref bean="sendDataServiceBean" />
		</jaxws:serviceBean>
		<jaxws:inInterceptors>
			<ref bean="inMessageInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
	</jaxws:server>

</beans>


web.xml

<listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 设置Spring容器加载配置文件路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext*.xml,/WEB-INF/classes/spring/spring-*.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>CXFService</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFService</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>



spring-service.xml
<bean id="TestSendDateService" class="testcxf.TestSendDataImpl">
		<property name="testjcDao">
			<ref bean="TestJmsCxfDao"/>
		</property>
	</bean>


客户端
package testcxf;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBea