Spring 整合WebService示例

Java常見的建立webService的方式有三種,Axis2(下載:http://axis.apache.org/axis2/java/core/download.cgi)、由JDK開發和Apache CXF等等。這裏寫一個CXF的例子。

首先是建立一個Web Service 工程,在配置文件web.xml中配置過濾器啓動項等

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!-- apach cxf -->
	<servlet>
		<servlet-name>CXFServlet</servlet-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>/services/*</url-pattern>
	</servlet-mapping>

	<!-- spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>sessionFactoryBeanName</param-name>
			<param-value>sessionFactory</param-value>
		</init-param>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>flushMode</param-name>
			<param-value>AUTO</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 已以下是自定義的過濾器,當然這是非必須的 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>com.gc.filter.StrutsFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

spring的容器的配置:以下是applicationContext.xml文件,請注意xmlns的應用問題

<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="
		   http://www.springframework.org/schema/beans 
		   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		   http://www.springframework.org/schema/tx 
		   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		   http://www.springframework.org/schema/aop 
		   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		   http://www.springframework.org/schema/context 
		   http://www.springframework.org/schema/context/spring-context-3.0.xsd
		   http://cxf.apache.org/jaxws 
		   http://cxf.apache.org/schemas/jaxws.xsd 
		   http://cxf.apache.org/jaxrs 
		   http://cxf.apache.org/schemas/jaxrs.xsd">    
		   
    <context:spring-configured />
    <context:component-scan base-package="com.gc" />

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	
	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="${jdbc.maxActive}"/>
		<property name="maxIdle" value="${jdbc.maxIdle}"/>
		<property name="maxWait" value="${jdbc.maxWait}"/>
		<property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
	</bean>

	<!-- 註解 sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan">
			<list>
				<value>com.gc.model</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.show_formate">true</prop>
			</props>
		</property>
	</bean>
	<!-- 配置 hibernateTemplate  -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 事務控制 -->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
 	<tx:annotation-driven transaction-manager="txManager" />
 	<aop:config>
		<aop:pointcut expression="execution(* com.gc.service.*.*(..))" id="bussinessService"/>
		<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice"/>
	</aop:config>
	<tx:advice id="txAdvice" transaction-manager="txManager">
	    <tx:attributes>
		   <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
		   <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED"/>
		   <tx:method name="regist*" propagation="REQUIRED"/>
		   <tx:method name="add*" propagation="REQUIRED"/> 
		   <tx:method name="save*" propagation="REQUIRED"/> 
		   <tx:method name="update*" propagation="REQUIRED"/> 
		   <tx:method name="del*" propagation="REQUIRED"/>
	    </tx:attributes>
	</tx:advice>
	
  	<!-- 配置 webservice 接口 -->
	<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="userManagerWs" implementor="com.gc.webservice.UserManagerImpl" address="/userManager"/>
  	
</beans>

另附上jdbc.properties和log4j.properties的文件配置

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh_cxf
jdbc.username=root
jdbc.password=root
jdbc.maxActive=100
jdbc.maxIdle=30
jdbc.maxWait=500
jdbc.defaultAutoCommit=true
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c:%L - %m%n
log4j.rootLogger=warn, stdout
log4j.logger.org.hibernate.tool.hbm2ddl=debug

至於java的文件的書寫方式和通常的ssh2開發相識,這裏面struts2用到的不多,可用可不用。下面是層次結構圖

  只是在spring的配置中加上webservice的內容,在webservice包中註解上webservice,代碼如下:

IUserManager接口:

package com.gc.webservice;

import java.util.List;

import javax.jws.WebService;

import com.gc.model.Product;
import com.gc.model.User;

@WebService
public interface IUserManager {
	public User getUser(String username, String password);
	public List<User> getUsers();
	public String addProduct(Product product);
	public List<Product> getProducts(String productCode, String productName, String userId);
	public Product getProductById(int productId);
	public String updateProduct(Product product);
	public void delProducts(String productIds);
}
userManager接口的實現類

package com.gc.webservice;

import java.util.List;

import javax.annotation.Resource;

import com.gc.model.Product;
import com.gc.model.User;
import com.gc.service.ProductService;
import com.gc.service.UserService;

public class UserManagerImpl implements IUserManager {
	private UserService userService;
	private ProductService productService;
	public UserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ProductService getProductService() {
		return productService;
	}
	@Resource
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	
	@Override
	public User getUser(String username, String password) {
		return userService.getUser(username, password);
	}

	@Override
	public List<User> getUsers() {
		return userService.getAllUsers();
	}

	@Override
	public String addProduct(Product product) {
		return productService.addProduct(product);
	}

	@Override
	public List<Product> getProducts(String productCode, String productName,
			String userId) {
		return productService.getProducts(productCode, productName, userId);
	}

	@Override
	public Product getProductById(int productId) {
		return productService.getProductById(productId);
	}

	@Override
	public String updateProduct(Product product) {
		return productService.updateProduct(product);
	}

	@Override
	public void delProducts(String productIds) {
		productService.delProducts(productIds);
	}

}


有希望仔細研究的同學可以下載源碼.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章