以前用xfrie,感觉不太好懂,现在用spring+xfire感觉很好理解。下面是个hello的例子。
IHello.java
java 代码
- package test;
- public interface IHello {
- public String helloTo(String name);
- }
HelloImpl.java
java 代码
- package test;
- public class HelloImpl implements IHello {
- public String helloTo(String name) {
- return "hello " + name + "!";
- }
- }
applicationContext.xml
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
- <beans>
- <bean
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="urlMap">
- <map>
- <entry key="/testService.ws">
- <ref bean="test" />
- </entry>
- </map>
- </property>
- </bean>
- <bean id="test" parent="webService"
- class="org.codehaus.xfire.spring.remoting.XFireExporter">
- <property name="serviceBean">
- <ref bean="testBean" />
- </property>
- <property name="serviceClass">
- <value>test.IHello</value>
- </property>
- </bean>
- <!-- webService base -->
- <bean id="webService"
- class="org.codehaus.xfire.spring.remoting.XFireExporter"
- abstract="true">
- <property name="serviceFactory">
- <ref bean="xfire.serviceFactory" />
- </property>
- <property name="xfire">
- <ref bean="xfire" />
- </property>
- </bean>
- <bean id="testBean" class="test.HelloImpl"></bean>
- </beans>
web.xml
java 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <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>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <listener>
- <listener-class>
- org.springframework.web.util.IntrospectorCleanupListener
- </listener-class>
- </listener>
- <servlet>
- <description>This is the description of my J2EE component</description>
- <display-name>This is the display name of my J2EE component</display-name>
- <servlet-name>Test</servlet-name>
- <servlet-class>test.Test</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>Test</servlet-name>
- <url-pattern>/servlet/Test</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
测试的action Test.java
java 代码
- package test;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- public class Test extends HttpServlet {
- /**
- * Constructor of the object.
- */
- public Test() {
- super();
- }
- /**
- * Destruction of the servlet. <br>
- */
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- /**
- * The doPost method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to post.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
- IHello hello = (IHello)wc.getBean("testBean");
- System.out.println(hello.helloTo("wcq"));
- }
- /**
- * Initialization of the servlet. <br>
- *
- * @throws ServletException if an error occure
- */
- public void init() throws ServletException {
- // Put your code here
- }
- }