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);
	}

}


有希望仔细研究的同学可以 下载源码.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值