1,新建maven項目,一路next,到finish(注意模板選擇)
使用mavn-archetype-webapp方式生成的模板,會提示project facets錯誤,可右鍵項目打開properties,修改project facets屬性(默認修改此選項時,apply不可點擊,可先取消勾選,點擊apply,再選中3.0,此時apply可點擊)
2,打開pom.xml文件,引入依賴關系(spring包需要引入完整,否則無法啟動)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java</groupId> <artifactId>simpleSpring</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>simpleSpring Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.0.5.RELEASE</spring.version> <cxf.version>2.7.15</cxf.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> <!-- 關閉(排除依賴引用)默認的commons-logging --> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- spring aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <!-- spring上下文 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!--若項目中包含jsp文件,需要添加servlet依賴--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- 以下為webservice依賴 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> <type>jar</type> </dependency> <!-- webservice end --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>simpleSpring</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <uriEncoding>utf-8</uriEncoding> <path>/simple</path> <port>80</port> </configuration> </plugin> </plugins> </build> </project>
3,配置web.xml文件
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>simple</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- 這個必須有,為程序啟動入口,作用是啟動Web容器時,自動裝配ApplicationContext的配置信息--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>cxfServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxfServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app>
PS:若缺少listener,訪問時后台報錯:WARNING: Can't find the the request for http://localhost/simple/webservice/webserviceHello's Observer
4,配置spring文件
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 配置bean自動掃描包位置 --> <context:component-scan base-package="com.java" /> <import resource="classpath:META-INF/cxf/cxf.xml"></import> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- id=hello對應webservice的servicename,implementor對應bean名稱,address為發布地址 --> <jaxws:endpoint id="hello" implementor="#helloClass" address="/webserviceHello"></jaxws:endpoint> </beans>
5,編輯代碼
package com.java; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface HelloWord { @WebMethod public String sayHi(@WebParam(name="name")String name); }
package com.java; import javax.jws.WebService; import org.springframework.stereotype.Component; @WebService(endpointInterface="com.java.HelloWord",serviceName="hello") @Component("helloClass") public class HelloWordImpl implements HelloWord { @Override public String sayHi(String name) { return "hello word "+name; } }
6,啟動項目,訪問路徑http://localhost/simple/webservice/webserviceHello?wsdl
其中simple為項目名稱,webservice為web.xml中配置,webserviceHello為spring.xml中配置的地址
ps:完整項目目錄http://files.cnblogs.com/files/javadongx/simpleSpringWebservice.rar