在運行測試時你有可能注意到
log4j:WARN No appenders could be found for logger (org.apache.cxf.bus.spring.BusApplicationContext).
log4j:WARN Please initialize the log4j system properly.
這是由於沒有加入log4j配置文件的緣故,通常狀況下,配置文件應當放在運行時的classpath中,即項目的target目錄中。但若是運行mvn clean的話target文件夾將被刪去,若是手動拷貝配置文件的話,麻煩不說還有可能形成版本的不一致。此時應當使用maven提供的資源管理功能,即將log4j.properties放在src/main/resources中,maven會自動將該文件放到項目的target目錄中(詳情參見上文中關於maven 命令lifecycle的講解),若是配置文件只在測試中被使用可將其放在src/test/resources中。
將spring的配置文件client-beans.xml放入src/main/resources中,代碼詳細附錄(二)
將org.world.hello.apps.cxf.bookstore.test.BookStoreServiceTest文件中的setUp方法替換爲
public
void setUp() {
// JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// factory.getInInterceptors().add(new LoggingInInterceptor());
// factory.getOutInterceptors().add(new LoggingOutInterceptor());
// factory.setServiceClass(BookStore.class);
// factory.setAddress("http://localhost:9000/bookStore");
// setService((BookStore) factory.create());
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(
new String[]{"client-beans.xml"});
service
= (BookStore)context.getBean("client");
}
並添加
import org.springframework.context.support.ClassPathXmlApplicationContext;
啓動(/重啓)Server,執行mvn test驗證一切正常。
你有可能會問‘我並無在pom.xml中引入spring的包,爲何可使用spring呢?’;答案在於你所用的包是由maven從repository中下載過來的,其依賴性也由maven所管理,而cxf項目自己便依賴與spring框架,因此spring也被加入到你的classpath中了;你能夠從eclipse中看到你的項目依賴於哪些包。
建立src/main/webapp/WEB-INF文件夾。
建立文件src/main/webapp/WEB-INF/web.xml,內容以下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
</web-app>
建立文件src/main/webapp/index.html,內容以下:
<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content=
"text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
Hello from index.html
</body>
</html>
修改pom.xml,添加如下內容(添加了jetty的maven插件,並指定了在9090端口啓動jetty)
<plugins>
……
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<connectors>
<connector
implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port>
</connector>
</connectors>
</configuration>
</plugin>
……
</plugins>
<finalName>start_off</finalName>
執行命令
$mvn jetty:run
你將能看到「Hello from index.html」字樣,啓動的server可用ctrl+c來中止。
修改web.xml,內容以下
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<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>/services/*</url-pattern>
</servlet-mapping>
</web-app>
其中指定了使用WEB-INF/beans.xml做爲cxf的配置文件,並將服務掛載在/services/*上。
下面建立WEB-INF/beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
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 address="/bookStore" serviceName="t:bookStoreService"
xmlns:t="http://bookstore.hello.world.org">
<jaxws:implementor>
<bean
class=
"org.world.hello.apps.cxf.bookstore.server.BookStoreServiceImpl" />
</jaxws:implementor>
<jaxws:inInterceptors>
<bean
class=
"org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean
class=
"org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>
jaxws:implementor中指定了服務的實現者。
執行命令
$mvn jetty:run
修改client-beans.xml將proxyFactory的address屬性從新指定爲「http://127.0.0.1:9090/start_off/services/bookStore」
在另外一個命令行
$mvn test
若是經過測試,說明服務已經正常發佈在jetty上了。
在pom.xml中更改打包的格式,即將<packaging>jar</packaging>改成<packaging>war</packaging>。
執行命令
$mvn package
*
注:
package
指令在
maven
中的生命期在
test
以後,因此執行
package
時會自動執行
test
,即只有經過了測試才能打包。
執行完打包指令以後,在項目的target目錄中就能夠看到start_off-1.0.war,要將工程部署在tomcat上,只需將war文件拷貝到webapps文件夾中便可。
修改pom.xml中的<packaging>war</packaging>爲<packaging>pom</packaging>
執行命令(要在同一行中寫)
mvn archetype:create -Dversion=1.0 -DgroupId=org.world.hello.apps -DartifactId=start_off_api
執行以上命令後,在start_off項目的pom.xml中會自動添加<modules><module>start_off_api</module></modules>
,代表start_off_api項目爲本項目的子項目。
同時在start_off項目的文件夾中會生成start_off_api項目的文件結構。
查看start_off_api項目的pom.xml,也能夠看出它和start_off項目的關係。
<parent>
<artifactId>start_off</artifactId>
<groupId>org.world.hello.apps</groupId>
<version>1.0</version>
</parent
執行命令
$mvn eclipse:eclipse
能夠發現一條指令同時在操做兩個項目。
輸出以下結果:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] start_off ............................................. SUCCESS [3.734s]
[INFO] start_off_api ......................................... SUCCESS [5.531s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9 seconds
[INFO] Finished at: Mon Nov 26 10:24:54 CST 2007
[INFO] Final Memory: 9M/17M
以後可將start_off_api項目也導入eclipse。
同理建立start_off_web項目。
執行命令(要在同一行中寫)
mvn archetype:create -Dversion=1.0 -DgroupId=org.world.hello.apps -DartifactId=start_off_web
將沒有用的App.java和AppTest.java刪除。
將start_off項目的src/main/webapp文件夾移到start_off_web項目中。
將start_off項目src中的剩餘內容移到start_off_api項目的src文件夾中。
註釋掉org.world.hello.apps.cxf.bookstore.test. BookStoreServiceTest中的有效測試,添加一個假的測試(目的是跳過maven生命週期的測試階段,而執行下面任務)
public
void testTruth() {
assertTrue(
true);
}
修改start_off_web項目的pom.xml文件,修改後的內容以下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>start_off</artifactId>
<groupId>org.world.hello.apps</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.world.hello.apps</groupId>
<artifactId>start_off_web</artifactId>
<name>start_off_web</name>
<version>1.0</version>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.world.hello.apps</groupId>
<artifactId>start_off_api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<finalName>start_off_web</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<connectors>
<connector
implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port>
</connector>
</connectors>
</configuration>
</plugin>
</plugins>
</build>
</project>
*
注:其中指出了start_off_web
項目依賴於
start_off_api
項目
修改start_off_api項目的pom.xml文件,修改後的內容以下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>start_off</artifactId>
<groupId>org.world.hello.apps</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.world.hello.apps</groupId>
<artifactId>start_off_api</artifactId>
<name>start_off_api</name>
<version>1.0</version>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
org.world.hello.apps.cxf.bookstore.bin.Server
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
*
注:子項目所依賴的包可從父項目中間接獲得
在start_off項目中的pom.xml中刪去插件maven-jetty-plugin和maven-jar-plugin。
執行命令
$mvn clean eclipse:clean eclipse:eclipse
$mvn install
以上的install命令,將項目安裝到了本地的repository中。
若想將服務部署到其餘server上,可在start_off項目的根目錄下執行mvn package,生成的war被放在start_off_web項目的target目錄中。
建立start_off_impl子項目,執行命令(要在同一行中寫)
mvn archetype:create -Dversion=1.0 -DgroupId=org.world.hello.apps -DartifactId=start_off_impl
將沒有用的App.java和AppTest.java刪除。
修改start_off_impl項目的pom.xml以下
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>start_off</artifactId>
<groupId>org.world.hello.apps</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.world.hello.apps</groupId>
<artifactId>start_off_impl</artifactId>
<name>start_off_impl</name>
<version>1.0</version>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.world.hello.apps</groupId>
<artifactId>start_off_api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
修改start_off_web項目的pom.xml,添加依賴
<dependency>
<groupId>org.world.hello.apps</groupId>
<artifactId>start_off_impl</artifactId>
<version>1.0</version>
</dependency>
將start_off_api中的
org.world.hello.apps.cxf.bookstore.server. BookStoreServiceImpl.java
org.world.hello.apps.cxf.bookstore.test. BookStoreServiceTest.java
org.world.hello.apps.cxf.bookstore.bin.Server.java
移動到start_off_impl項目中。
在start_off項目的根目錄下執行命令
$mvn clean eclipse:clean eclipse:eclipse install
以後即可啓動jetty,驗證服務仍可訪問,項目拆分完畢。