webservice 基於struts2 spring3 hibernate4 cxf2.7
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0"> <display-name>webservicetest</display-name> <description>webservicetest</description> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- CXF --> <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>/webServices/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> <!-- struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-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> <!-- log4j --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> </web-app>
寫介面
package org.fkit.webService;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHello(String name);
}
package org.fkit.webService.impl;
import javax.jws.WebService;
import org.fkit.webService.HelloWorld;
@WebService(endpointInterface = "org.fkit.webService.HelloWorld",serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
System.out.println("hello " + name);
return "hello " + name;
}
}
建好了類,需要裝配到spring中,讓spring來管理,applicationContext-commons.xml檔案中加入如下程式碼:
xmlns:jaxws=http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
<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="webServiceHelloWorld" address="/HelloWorld" implementor="org.fkit.webService.impl.HelloWorldImpl"/>
<?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"
<span style="color: rgb(204, 102, 0);">xmlns:jaxws="http://cxf.apache.org/jaxws"</span> xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
<span style="color: rgb(204, 102, 0);">http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd</span>">
<context:annotation-config />
<context:component-scan base-package="org.fkit" />
<span style="color: rgb(204, 102, 0);"><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="webServiceHelloWorld"
address="/HelloWorld"
implementor="org.fkit.webService.impl.HelloWorldImpl"/></span>
<!-- bean definitions here -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${connection.driver_class}" />
<property name="jdbcUrl" value="${connection.url}" />
<property name="user" value="${connection.username}" />
<property name="password" value="${connection.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="c3p0.min_size">${c3p0.min_size}</prop>
<prop key="c3p0.max_size">${c3p0.max_size}</prop>
<prop key="c3p0.max_statements">${c3p0.max_statements}</prop>
<prop key="c3p0.timeout">${c3p0.timeout}</prop>
</props>
</property>
<property name="configLocations">
<list>
<value>classpath:hibernate.cfg.xml</value>
</list>
</property>
</bean>
<!-- more bean definitions go here -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事務處理 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="modify*" />
<tx:method name="edit*" />
<tx:method name="delete*" />
<tx:method name="remove*" />
<tx:method name="repair" />
<tx:method name="deleteAndRepair" />
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="datagrid*" read-only="true" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* org.fkit.service.impl.*.*(..))"
id="transactionPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
</aop:config>
</beans>
<?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"
<span style="color: rgb(204, 102, 0);">xmlns:jaxws="http://cxf.apache.org/jaxws"</span> xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
<span style="color: rgb(204, 102, 0);">http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd</span>">
<context:annotation-config />
<context:component-scan base-package="org.fkit" />
<span style="color: rgb(204, 102, 0);"><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="webServiceHelloWorld"
address="/HelloWorld"
implementor="org.fkit.webService.impl.HelloWorldImpl"/></span>
<!-- bean definitions here -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${connection.driver_class}" />
<property name="jdbcUrl" value="${connection.url}" />
<property name="user" value="${connection.username}" />
<property name="password" value="${connection.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="c3p0.min_size">${c3p0.min_size}</prop>
<prop key="c3p0.max_size">${c3p0.max_size}</prop>
<prop key="c3p0.max_statements">${c3p0.max_statements}</prop>
<prop key="c3p0.timeout">${c3p0.timeout}</prop>
</props>
</property>
<property name="configLocations">
<list>
<value>classpath:hibernate.cfg.xml</value>
</list>
</property>
</bean>
<!-- more bean definitions go here -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事務處理 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="modify*" />
<tx:method name="edit*" />
<tx:method name="delete*" />
<tx:method name="remove*" />
<tx:method name="repair" />
<tx:method name="deleteAndRepair" />
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="datagrid*" read-only="true" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* org.fkit.service.impl.*.*(..))"
id="transactionPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
</aop:config>
</beans>
還需要配置一下struts2,不然會報錯,不能找到action,我的這個配置檔案是給其它配置檔案繼承的,程式碼是為webservice配置的。
<package name="webServices" extends="vcs-base" namespace="/webServices">
<action name="*">
<result>{1}</result>
</action>
</package>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="vcs-base" extends="struts-default">
<!-- 配置全域性結果 -->
<global-results>
<result name="index">/index.jsp</result>
</global-results>
</package>
<span style="color: rgb(204, 102, 0);"><package name="webServices" extends="vcs-base" namespace="/webServices">
<action name="*">
<result>{1}</result>
</action>
</package>
</span></struts>
現在可以啟動tomcat了。啟動完成後,訪問http://localhost:8080/WebserviceTest/webServices/HelloWorld?wsdl
如果顯示出wsdl的內容,說明配置完成