时间:2016-10-27 05:22:50

标签: java xstream

我在同一个类中使用多个XStream个实例。

XStream工厂的以下实现是否是线程安全的,是否有任何重大的性能影响?

public final class XStreamFactory {

    private XStreamFactory() {
    }

    public static XStream getXstream(Class<?> clazz) {
        XStream xstream = new XStream() {
            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    @Override
                    public boolean shouldSerializeMember(@SuppressWarnings("rawtypes") Class definedIn, String fieldName) {
                        if (definedIn == Object.class)
                            return false;

                        return super.shouldSerializeMember(definedIn, fieldName);
                    }
                };
            }
        };

        if (clazz != null) {
            xstream.autodetectAnnotations(Boolean.TRUE);
            xstream.processAnnotations(clazz);
        }

        return xstream;
    }
}

将XML转换为Java

Public class Resposne {

    private XStream xstream1=XStreamFactory.getXstream(XClass1.class);
    private XStream xstream2=XStreamFactory.getXstream(XClass2.class);

    public XClass1 readReplyOne(String reply1){
        return (XClass1) xstream1.fromXML(reply1)
    }

    public XClass2 readReplyOne(String reply2){
        return (XClass2) xstream2.fromXML(reply2)
    }
}

0 个答案:

没有答案