java webservice之xfire与spring2集成(三)

由于xfire的支持库中是以spring1.2.6.jar 作为spring 的支持库所以这种方式不能正常的集成.

所以:

1: 删除xfire下的spring1.2.6. jar再加入spring2的支持库。

2:在web.xml中编写如下代码

 

 <!-- 配置文件路径 开始-->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath:applicationContext.xml,
   classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
 </context-param>
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:log4j.properties</param-value>
 </context-param>


 <!-- 启动时加载SpringContextServlet-->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener
  </listener-class>
 </listener>
 <listener>
  <listener-class>org.springframework.web.util.IntrospectorCleanupListener
  </listener-class>
 </listener>


 <!-- XFire 配置-->
 <servlet>
  <servlet-name>XFireServlet</servlet-name>
  <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet
  </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>XFireServlet</servlet-name>
  <url-pattern>/servlet/XFireServlet/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>XFireServlet</servlet-name>
  <url-pattern>/services/*</url-pattern>
 </servlet-mapping>

classpath:org/codehaus/xfire/spring/xfire.xml是一定要加入到context-param中的.

3: 编写服务的接口和具体的实现类.举例如下:

package com.wujianjun.xfire.spring;

import java.util.List;

import com.wujianjun.xfire.domain.Person;

public interface IPersonService {
	Person getMyInfo(String name);
	String sayHello(String name);
	List<Person> getPersons(String name);
}

 

package com.wujianjun.xfire.spring;

import java.util.ArrayList;
import java.util.List;

import com.wujianjun.xfire.domain.Person;

public class PersonServiceImpl implements IPersonService {
	
	private String job;

	public void setJob(String job) {
		this.job = job;
	}

	@Override
	public Person getMyInfo(String name) {
		Person res = new Person();
		res.setJob(job);
		res.setName(name);
		return res;
	}
	
	@Override
	public String sayHello(String name) {
		return "你好: "+name+",job="+job;
	}

	@Override
	public List<Person> getPersons(String name) {
		List<Person> res = new ArrayList<Person>();
		Person p = new Person();
		p.setJob("卖饼的");
		p.setName("大郎");
		res.add(p);
		p = new Person();
		p.setJob("卖水的");
		p.setName(name);
		res.add(p);
		return res;
	}

}

 

4: 编写applicationContext.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 id="personService" class="com.wujianjun.xfire.spring.PersonServiceImpl">
  <property name="job" value="JAVA" />
 </bean>
 <bean name="PersonService" class="org.codehaus.xfire.spring.ServiceBean" >
        <property name="serviceBean" ref="personService" />
        <property name="serviceClass" value="com.wujianjun.xfire.spring.IPersonService" /> 
  </bean> 
 
</beans>

5:发布程序,启动服务器访问http://localhost:8080/xfire/services/则可以看到我们发布的服务。如果我们做了配置而没有显示服务,我们要看一下我们applicationContext.xml里面的bean 是否有lazy-init="false"的。如果有,则我们要把以上的服务配置改成

 

<?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 id="personService" class="com.wujianjun.xfire.spring.PersonServiceImpl" lazy-init="false">
  <property name="job" value="JAVA" />
 </bean>
 <bean name="PersonService" class="org.codehaus.xfire.spring.ServiceBean" lazy-init="false">
        <property name="serviceBean" ref="personService" />
        <property name="serviceClass" value="com.wujianjun.xfire.spring.IPersonService" /> 
  </bean> 
 
</beans>

点击旁边的wsdl则可以访问到对应的wsdl文件了。

再通过我们前面的调用就可以了。

 

如果有复杂类型的返回值,则要在对应接口的同一个包下建立一个InterfaceName.aegis.xml文件其对应的写法如下:

*.aegis.xml文件的方法映射编写
1、void setList(int id ,java.util.List,boolean persist);
 <method name="setList"  >
        <parameter index="1" componentType="java.lang.String" />
        <parameter index="2" class="boolean" />
    </method>
2、Collection getCollection();
 <method name= "getCollection" >
        <return-type componentType="java.lang.Double" />
    </method>
3、Map getGiftList() ;
 <method name= "getGiftList" >
  <return-type keyType= "org.codehaus.xfire.NiceChild" componentType= "org.codehaus.xfire.Present" >
    </method>
4、  public List getListOfListOfDoubles {
     List l = new ArrayList();
     List doubles = new ArrayList();
     doubles.add(new Double (1.0));
     l.add(doubles);
     return l;
 }
 <mapping>
     <method name= "getListofListofDoubles" >
       <return-type componentType="#someDoubles" />
     </method>
     <component name="someDoubles" class="java.util.List" componentType="java.lang.Double" />
 </mapping>

你可能感兴趣的:(java,spring,bean,servlet,webservice)