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
<?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
<?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
package xx;

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

HelloWorldService.java
package xx;

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

你可能感兴趣的:(Web,service)