Apache CXF + Spring

比较了Spring WS和Apache CXF,觉得Spring WS属于contract-first类型的WebService,自己不熟悉xsd,
所以使用Apache CXF这个组件,而且它的功能更为强大并且可以与Spring整合。
部署在tomcat上,然后访问 http://127.0.0.1:8080/foo/HelloWorld?wsdl

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<context:component-scan base-package="xx" />
<context:annotation-config />

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="someDB" />
</bean>


<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<tx:advice id="syncAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="syncOperation"
expression="execution(public * xx..*Service.*(..))" />
<aop:advisor advice-ref="syncAdvice"
pointcut-ref="syncOperation" />
</aop:config>

<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" />

<jaxws:endpoint id="helloWorld" implementor="#helloWorldService"
address="/HelloWorld" />

<jaxws:client id="helloWorldClient"
serviceClass="xx.HelloWorld"
address="http://localhost:8080/shape/shape2sp" />

</beans>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

HelloWorld.java

1
2
3
4
5
6
package xx;

@WebService
public interface HelloWorld {
void sayHi(String user);
}

HelloWorldService.java

1
2
3
4
5
6
7
8
9
package xx;

@Service
@WebService(endpointInterface = "xx.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldService implements HelloWorld{
public void sayHi(String user){
System.out.println("hello: " + user);
}
}