Log4j.properties 在 XFire Web Service project 中 位置
http://blog.csdn.net/caok/archive/2008/11/20/3339260.aspx
使用XFire开发WebService,环境:Myeclipse5.0GA (eclipse3.2) XFIRE1.1 Tomcat
1. 在Myeclipse中新建一个WEB Service PROJECT,如XFIRE2007。
2. 选中工程XFIRE2007,右键导入Myeclipse--Add Spring Capabilities....,所有均选择默认值。
3. 以上步骤完成后,修改src目录下的applicationContext.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="echoBean" class="cutelion.xfire.DIYEchoImpl" />
</beans>
4. 在src目录下新建log4j.properties,内容如下:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
5. 修改WEB-INF目录下的web.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- <servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml
classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
6. 在WEB-INF目录下建立一个xfire-servlet.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- START SNIPPET: xfire -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/EchoService">
<ref bean="echo"/>
</entry>
</map>
</property>
</bean>
<!-- Declare a parent bean with all properties common to both services -->
<bean id="echo" class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory">
<ref bean="xfire.serviceFactory"/>
</property>
<property name="xfire">
<ref bean="xfire"/>
</property>
<property name="serviceBean">
<ref bean="echoBean"/>
</property>
<property name="serviceClass">
<value>cutelion.xfire.DIYEcho</value>
</property>
</bean>
<!-- END SNIPPET: xfire -->
</beans>
7. 在src下面建立cutelion.xfire包,用于服务发布, 其中建立一个DIYEcho.java接口和DIYEchoImpl.java类实现。内容如下:
接口: DIYEcho.java
// START SNIPPET: echo
package cutelion.xfire;
public interface DIYEcho
{
String DIYecho(String in);
}
// END SNIPPET: echo
接口实现: DIYEchoImpl.java
// START SNIPPET: echo
package cutelion.xfire;
import java.text.DateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Provides a default implementation of the echo service interface.
*/
public class DIYEchoImpl implements DIYEcho
{
public String DIYecho(String in)
{
GregorianCalendar today = new GregorianCalendar();
Date d = today.getTime();
DateFormat df = DateFormat.getDateTimeInstance();
String todaytostring = df.format(d);
return todaytostring+" 輸入:"+in;
}
}
// END SNIPPET: echo
8. 到这里现在可以部署服务了, 工程右键Myeclipse-->Add and Move Project Deployment..... 到 Tomcat 下面。重启Tomcat。
9. 一切OK,Tomcat启动正常,没有错误。可以访问WSDL,在浏览器中输入:
http://127.0.0.1:8080/XFIRE2007/EchoService?wsdl出现一大堆的XML,表明服务EchoService发布成功.
在浏览器中输入:http://127.0.0.1:8080/XFIRE2007/EchoService 出现
Invalid SOAP request. OK!
以上表明服务发布成功。以下为客户端的测试:
为了充分体现SPRING的IOC特性,现做以下测试。
10. 在src下建立一个applicationContext-client.xml 配置文件.内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="Client_Params" class="cutelion.xfire.client.ClientParams">
<property name="訪問地址">
<value>http://localhost:8080/XFIRE2007/EchoService</value>
</property>
<property name="輸入字符串">
<value>1234567890ABCDEFG XFIRE與SPRING集成成功! </value>
</property>
</bean>
</beans>
11. 在src下建立Package: cutelion.xfire.client,其中建立一个客户端参数的类ClientParams.java 和测试服务的类 TestDIYEcho.java 其内容如下所示:
ClientParams.java
package cutelion.xfire.client;
public class ClientParams {
private String 輸入字符串;
private String 訪問地址;
public String get輸入字符串() {
return 輸入字符串;
}
public void set輸入字符串(String 輸入字符串) {
this.輸入字符串 = 輸入字符串;
}
public String get訪問地址() {
return 訪問地址;
}
public void set訪問地址(String 訪問地址) {
this.訪問地址 = 訪問地址;
}
}
TestDIYEcho.java
package cutelion.xfire.client;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import cutelion.xfire.DIYEcho;
import cutelion.xfire.client.ClientParams;
public class TestDIYEcho {
public static void main(String args[]) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext-client.xml");
ClientParams cp=(ClientParams)ctx.getBean("Client_Params");
String inputString=cp.get輸入字符串();
Service srvcModel = new ObjectServiceFactory().create(DIYEcho.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
//String url = "http://localhost:8080/XFIRE2007/EchoService";
String url = cp.get訪問地址();
try {
DIYEcho srvc = (DIYEcho) factory.create(srvcModel, url);
System.out.print(srvc.DIYecho(inputString));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
12. 完成TestDIYEcho.java的编写后,直接进行测试( alt+shift+X J )运行:
结果为:
2007-1-18 15:37:30 輸入:1234567890ABCDEFG XFIRE與SPRING集成成功