Xfire的aegis绑定方式配置


当遇到需要映射POJO或配置WebService接口方法返回类型(如集合类型)时,需要进行绑定,此时aegis的绑定为建立.aegis.xml的文件。


public interface UserInfo {

	public User findUserInfosByUserID(String userUniqueID);
	public String test();
	public Collection findRoleInfosByUserID(String userUniqueID);
	}

观察此接口,受限我们看findRoleInfosByUserID方法,此方法传入String,返回类型为Collection,此时,我们即需要配置findRoleInfosByUserID方法。
配置步骤,在接口类的同一包下建立和接口名相同的aegis.xml文件,即UserInfo.aegis.xml文件。
UserInfo.aegis.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings>
	<mapping>
		<method name="findRoleInfosByUserID">
			<return-type
				componentType="com.tongtech.bjvsp.sysmng.entity.Role" />
		</method>
	</mapping>
</mappings>

其中,method代表需要配置的方法名,由于XFire可以支持基本类型,针对集合类型时,需要配置,返回类型或参数类型为基本类型时不需要特殊配置。
<return-type/>为配置返回类型,componentType属性代表返回的集合中所包含的类,此类可以是自定义的对象,也可以是基本类型,例如java.lang.String
此时,findRoleInfosByUserID方法配置完毕  

PS,还拿此接口举例,如接口中,findRoleInfosByUserID接受的参数也为集合类型,那么需要在<method></method>之间再加入以下代码

<parameter index="0" componentType="java.lang.String"/>

index="0"代表方法的参数的序号,0即第一个参数。
componentType代表集合参数内包含的类型,也可以为自定义对象,例如User
注意,如果findRoleInfosByUserID方法有两个参数,一个比如为String,一个为集合,那么只需要配置集合参数即可。 


二、映射POJO
比如还是上面的UserInfo接口,第一个方法
public User findUserInfosByUserID(String userUniqueID);
此时,返回类型为User
如果User中仅为基本类型,例如String,int等基本类型,无需配置User

但如果User中还包含有其他类型,比如包含有Set,List,那么需要对其进行配置
比如User代码如下:

public class User  implements java.io.Serializable {


    // Fields    

     private Integer userId;
     private String loginName;
     private String userPassword;
     private Integer SOrder;
     private String userCa;
     private String userName;
     private String userTel;
     private String userEmail;
     private String ZNote;
     private String userCode;
     private DicSystemAuth SDicSystemAuth;
     private Set SGroupUsers = new HashSet(0);
     private Set SUserDepts = new HashSet(0);
}
此时User中还包含有SDicSystemAuth的对象,以及SUserDepts等Set集合
这种情况主要可能会出现在使用Hibernate,映射一对一,一对多表时
这里要特别注意
那么此时我们需要对User进行aegis绑定,控制POJO和XML的映射
配置方法:
在User对象所在包内建立User.aegis.xml
由于User中包含Set,那么只需要对集合做设置即可,DicSystemAuth下面单独讨论

我们可以分两次来配置,分别讲解 
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns:my="http://my.bjvsp.tongtech.com">
<mapping name="my:User">
<property name="SGroupUsers" componentType="com.tongtech.bjvsp.sysmng.entity.GroupUser"/>
<property name="SUserDepts" componentType="com.tongtech.bjvsp.sysmng.entity.UserDept"/>
</mapping>
</mappings>
此时,配置文件中配置了两个Set集合,componentType明确定义了Set集合内包含的内容
此时,DicSystemAuth即便是自定义的对象类,由于不是集合,XFire也可以对其正确映射。


<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns:my="http://my.bjvsp.tongtech.com">
<mapping name="my:User">

<property name="SGroupUsers" ignore="true"/>
<property name="SUserDepts" ignore="true"/>
<property name="SDicSystemAuth" ignore="true"/>
</mapping>
</mappings>

第二种方式,定义了三个字段,并使用了ignore属性,表示这三个属性不能被序列化。
那么,何时会遇到类似于User中Set集合,不被序列化呢?

当客户端访问本地代理时,如果User表内包含有其他表的关联时,如果基于hibernate的持久层,由于hibernate的惰性加载机制,加载User时仅会将User中的属性值加载而不会加载关联表中的值,比如set和SDicSystemAuth对象等,当客户端访问本地代理,序列化User对象时,如果没有设置关联字段是否序列化,那么它会将其全部序列化,但当序列化时,由于hibernate的惰性加载,此时Session已经关闭,那么在序列化关联字段的时候,就会出现session was close的异常。因此解决办法是在aegis绑定时制定关联字段的ignore属性设置为true。

还有一种解决办法,就是仍然按照第一种配置方式,明确指明关联的Set集合内的对象,但在执行序列化前,session关闭之前,提前将关联字段内的值初始化。



在WebServices的开发中,通常要处理处长复杂的类型,如返回的是Collection类,或参数是Collection类,或返回的是自定义对象类型,或者参数是自定义对象类型的类,都需要编写className.aegis.xml文件,这种处理方式与axis差不多一样,只不过axis是在service.wsdd中配置。
如有以下接口

package com.efs.xfire.pojo;
import java.util.*;
public interface CollectionsDemo {
public int getCollectionsRowCount(List list);
public List getUserList();
}



在该接口的同一包下,需进行如下配置

<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping>
<method name="getCollectionsRowCount">
<parameter index="0" componentType="java.lang.String"/>
</method>
<!-- 返回的类型是Map的话,做法和List一样。但定义的类型,是Map中的Value部分 -->
<method name="getUserList"> 
<return-type componentType="com.efs.xfire.entity.User"/>
</method>
</mapping>
</mappings>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值