67,513
社区成员
发帖
与我相关
我的任务
分享
package com.sdo.webservice.webservice;
import javax.jws.WebService;
import com.sdo.webservice.entity.Student;
@WebService
public interface StudentWebService {
Student getStudentById(int id);
}
这是webservice实现类
[code=java]
package com.sdo.webservice.webservice.impl;
import javax.annotation.Resource;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sdo.webservice.dao.StudentDao;
import com.sdo.webservice.entity.Student;
import com.sdo.webservice.webservice.StudentWebService;
@WebService(endpointInterface = "com.sdo.webservice.webservice.StudentWebService",serviceName="Student")
@Service
public class StudentWebServiceImpl implements StudentWebService{
@Resource
private StudentDao studentDao;
@Override
public Student getStudentById(int id) {
return studentDao.getStudentById(id);
}
}
package com.sdo.webservice.dao;
import com.sdo.webservice.entity.Student;
public interface StudentDao {
Student getStudentById(int id);
}
<?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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
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-servlet.xml" />
<jaxws:endpoint id="Student" address="/StudentWebService"
implementor="com.sdo.webservice.webservice.impl.StudentWebServiceImpl" />
</beans>
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 采用注释的方式配置bean -->
<context:annotation-config/>
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.sdo.webservice"/>
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:config/init.properties"/>
<!-- 配置数据源 oracle c3p0-->
<bean id="dataSourceTarget" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"
p:checkoutTimeout="${cpool.checkoutTimeout}"
p:minPoolSize="${cpool.minPoolSize}"
p:maxPoolSize="${cpool.maxPoolSize}"
p:maxIdleTime="${cpool.maxIdleTime}"
destroy-method="close"
></bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="dataSourceTarget"></property>
</bean>
<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/sqlmap-config.xml" />
<property name="mapperLocations" value="classpath:sqlMapper/*Mapper.xml" />
<property name="typeAliasesPackage" value="com.sdo.webservice.entity" />
</bean>
<!-- 配置dao层与mapper.xml关联 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.sdo.webservice.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/applicationContext.xml,
classpath:config/applicationContext-webservice.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</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>/*</url-pattern>
</servlet-mapping>
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.sdo.webservice.entity.Student;
import com.sdo.webservice.webservice.StudentWebService;
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
bean.setServiceClass(StudentWebService.class);
bean.setAddress("http://localhost:8080/CXF0621/StudentWebService");
StudentWebService studentWebService = (StudentWebService)bean.create();
Student student = studentWebService.getStudentById(1);
System.out.println(student.toString());
}
}
这是查询的结果
六月 21, 2017 9:44:50 上午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.webservice.sdo.com/}StudentWebServiceService from class com.sdo.webservice.webservice.StudentWebService
Student [id=1, name=xrq, sex=男, address=gggg]