标签:webservice java spring cxf web服务
很早很早之前,就初步学习了WebService,感觉还是比较“好”的。 @WebService
public interface UserFacade {
String query();
} package cn.fansunion.webservice.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.fansunion.webservice.UserFacade;
public class WebServiceTest {
public static void main(String[] args) {
//初始化Spring上下文,webservice文件位于src目录下(也可以说是Classpath下)
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:/spring-webservice.xml");
//根据id获得bean,我感觉是这个“jaxws:client id="remoteUserFacade"”WebService语法和Spring语法的结合
UserFacade userFacade=(UserFacade) ac.getBean("remoteUserFacade");
//查询,返回并打印字符串“WebService”
System.out.println(userFacade.query());
}
}<?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:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 启用autowire -->
<context:annotation-config />
<context:component-scan base-package="com.fansunion.webservice" />
<jaxws:client id="remoteUserFacade"
address="http://localhost:8080/webService/UserFacade" serviceClass="cn.fansunion.webservice.UserFacade" />
</beans><dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>package cn.fansunion.webservice.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Service;
import cn.fansunion.webservice.UserFacade;
@WebService(endpointInterface = "cn.fansunion.webservice.UserFacade", serviceName = "UserFacade")
@Service("userFacadeImpl")
public class UserFacdeImpl implements UserFacade {
@Override
public String query() {
return "WebService";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-webservice.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/webService/*</url-pattern>
</servlet-mapping>
</web-app><!-- Import CXF --> <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="userFacade" class="cn.fansunion.webservice.impl.UserFacdeImpl"> </bean> <jaxws:server id="webserviceUserFacade" serviceBean="#userFacade" address="/UserFacade"> </jaxws:server> <!-- 启用autowire --> <context:annotation-config /> <context:component-scan base-package="com.fansunion.webservice" />
版权声明:本文为博主原创文章,未经博主允许不得转载。
Java-Spring-WebService最基础的配置示例
标签:webservice java spring cxf web服务
原文地址:http://blog.csdn.net/fansunion/article/details/49455507