---
title: "castor 应用中遇到的问题"
author: "floydd"
date: "2007-10-13T20:50:00Z"
tags: []
slug: "castor"
draft: false
lastmod: "2011-12-26T12:03:21Z"
id: 1326388
url: "https://floydd.iteye.com/blog/1326388"
---

以下所有基本上都是从castor文档上copy过来的，只是在这次项目应用中遇到了这些问题，记录下。

castor虽然用起来很简单，可是用的好却不简单。这里只是提一些使用中的遇到的问题：

1.缩进问题和命名规范。

org.exolab.castor.indent=true  
org.exolab.castor.xml.naming=mixed

加上上面的第一个属性就能使产生的xml有缩进，格式比较好看（调试的时候可以用用，正式环境还是去掉比较好）

第二属性设置后，生成的xml的元素名称就可以达到基本符合java的命名规范。

还可以使用&lt;class name="com.acme.Foo" auto-complete="true"/&gt; auto-complete来化繁为简（不用写具体的绑定配置拉）

2.性能问题

castor每次在loadingMapping的时候都会生成ClassDescriptorResolver（大概就是用来将xml的类配置信息映射到对象），如果用castor的quick start里的那种方式可能会有性能问题（其实差的不是很多，经过过我测试，序列化500个对象采用这种方式和后面将要用的新方式只是查了3倍左右。平均时间：10ms&lt;-&gt;3ms），以下是缓存ClassDescriptorResolver的解决方案：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclassMappingUtils...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticLoglog=LogFactory.getLog(MappingUtils.class);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstatic&lt;T&gt;Tdeserialize(Readerreader,Class&lt;T&gt;clazz)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)URLmappingFile=clazz.getResource(clazz.getSimpleName()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)+"-mapping.xml");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ClassDescriptorResolverresolver=getCachedResolver(mappingFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Unmarshallerunmarshaller=newUnmarshaller();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)unmarshaller.setResolver((XMLClassDescriptorResolver)resolver);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnclazz.cast(unmarshaller.unmarshal(reader));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)log.error("",e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnnull;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidserialize(Writerwriter,Objectobj)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Classclazz=obj.getClass();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)serialize(writer,obj,clazz.getResource(clazz.getSimpleName()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)+"-mapping.xml"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)publicstaticvoidserialize(Writerwriter,Objectobj,URLmappingFile)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Marshallermarshaller=newMarshaller();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ClassDescriptorResolverresolver=getCachedResolver(mappingFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)marshaller.setResolver((XMLClassDescriptorResolver)resolver);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)marshaller.setWriter(writer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)marshaller.marshal(obj);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)log.error("",e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticMap&lt;Object,ClassDescriptorResolver&gt;resolverCache=newHashMap&lt;Object,ClassDescriptorResolver&gt;();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)privatestaticClassDescriptorResolvergetCachedResolver(URLmappingFile)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)throwsException...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ClassDescriptorResolverresolver=resolverCache.get(mappingFile  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif).getFile());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(resolver==null)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Mappingmapping=newMapping();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)mapping.loadMapping(mappingFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)resolver=ClassDescriptorResolverFactory  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif).createClassDescriptorResolver(BindingType.XML);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MappingUnmarshallermappingUnmarshaller=newMappingUnmarshaller();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MappingLoadermappingLoader=mappingUnmarshaller.getMappingLoader(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)mapping,BindingType.XML);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)resolver.setMappingLoader(mappingLoader);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)resolverCache.put(mappingFile.getFile(),resolver);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnresolver;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

3\. 直接序列化List对象

castor支持序列化一个list到xml！其实我也是试出来的，本来以为所有序列化对象都要是xml中的带有map-to顶层元素类。

你只需要定义list中对象的类映射到mapping文件就可以直接序列化一整个list了，大概像下面的这段代码

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicvoidserializeList(Writerwriter,Listobj,URLmappingFile)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)XMLContextcontext=newXMLContext();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Mappingmapping=XMLContext.createMapping();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)try...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)mapping.loadMapping(mappingFile);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)context.addMapping(mapping);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Marshallermarshaller=context.createMarshaller();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)marshaller.setSuppressXSIType(true);//去掉xsi:type  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)marshaller.setRootElement("root");//给顶层元素改个名字  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)marshaller.setWriter(writer);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)marshaller.marshal(obj);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)}catch(Exceptione)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)log.error("",e);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}