From 457a9809dbad871d1dd2eebbe8531083eacbec8a Mon Sep 17 00:00:00 2001 From: Daniel Qian Date: Wed, 15 Oct 2014 15:14:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AEJAXB=E5=AE=98=E6=96=B9?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=94=B9=E8=BF=9BJAXBContext=E7=9A=84?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/util/xml/XmlTransformer.java | 69 ++++++++++--------- .../chanjar/weixin/bean/WxXmlMessageTest.java | 4 +- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/main/java/me/chanjar/weixin/util/xml/XmlTransformer.java b/src/main/java/me/chanjar/weixin/util/xml/XmlTransformer.java index 49ff0516..b82161f8 100644 --- a/src/main/java/me/chanjar/weixin/util/xml/XmlTransformer.java +++ b/src/main/java/me/chanjar/weixin/util/xml/XmlTransformer.java @@ -8,84 +8,91 @@ import java.io.Writer; import java.util.HashMap; import java.util.Map; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; +import javax.xml.bind.*; +import me.chanjar.weixin.bean.*; import org.xml.sax.InputSource; import com.sun.xml.bind.marshaller.CharacterEscapeHandler; public class XmlTransformer { + protected static final JAXBContext jaxbContext = initJAXBContext(); + /** * xml -> pojo + * * @param clazz - * @param object + * @param xml * @return - * @throws JAXBException + * @throws JAXBException */ @SuppressWarnings("unchecked") public static T fromXml(Class clazz, String xml) throws JAXBException { - JAXBContext context = getJAXBContext(clazz); + JAXBContext context = jaxbContext; Unmarshaller um = context.createUnmarshaller(); T object = (T) um.unmarshal(new StringReader(xml)); return object; } - + @SuppressWarnings("unchecked") public static T fromXml(Class clazz, InputStream is) throws JAXBException { - JAXBContext context = getJAXBContext(clazz); + JAXBContext context = jaxbContext; Unmarshaller um = context.createUnmarshaller(); InputSource inputSource = new InputSource(is); inputSource.setEncoding("utf-8"); T object = (T) um.unmarshal(inputSource); return object; } - + /** * pojo -> xml + * * @param clazz + * @param object * @return - * @throws JAXBException + * @throws JAXBException */ public static String toXml(Class clazz, T object) throws JAXBException { StringWriter stringWriter = new StringWriter(); toXml(clazz, object, stringWriter); return stringWriter.getBuffer().toString(); } - + public static void toXml(Class clazz, T object, Writer writer) throws JAXBException { - JAXBContext context = getJAXBContext(clazz); + JAXBContext context = jaxbContext; Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(CharacterEscapeHandler.class.getName(), characterUnescapeHandler); m.marshal(object, writer); } - + protected static CharacterEscapeHandler characterUnescapeHandler = new CharacterUnescapeHandler(); - + protected static class CharacterUnescapeHandler implements CharacterEscapeHandler { public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException { writer.write(ac, i, j); } } - - protected static Map, JAXBContext> jaxbContexts = new HashMap, JAXBContext>(); - - protected static JAXBContext getJAXBContext(Class clazz){ - //JAXBContext是线程安全的,且创建相对比较耗性能,使用map缓存,并发下多次创建没问题. - JAXBContext context = jaxbContexts.get(clazz); - if(context == null){ - try { - context = JAXBContext.newInstance(clazz); - } catch (JAXBException e) { - throw new RuntimeException("创建JAXBContext实例失败:" + clazz.getName(), e); - } - jaxbContexts.put(clazz, context); - } - return context; + + private static JAXBContext initJAXBContext() { + /* + * JAXBContext对象是线程安全的,根据官方文档的建议将对象作为全局实例 + * https://jaxb.java.net/guide/Performance_and_thread_safety.html + */ + try { + return JAXBContext.newInstance( + WxXmlOutMessage.class, + WxXmlOutImageMessage.class, + WxXmlOutMewsMessage.class, + WxXmlOutMusicMessage.class, + WxXmlOutTextMessage.class, + WxXmlOutVideoMessage.class, + WxXmlOutVoiceMessage.class, + WxXmlMessage.class); + } catch (JAXBException e) { + throw new RuntimeException(e); + } } - + } diff --git a/src/test/java/me/chanjar/weixin/bean/WxXmlMessageTest.java b/src/test/java/me/chanjar/weixin/bean/WxXmlMessageTest.java index 6db20bd1..79ad7634 100644 --- a/src/test/java/me/chanjar/weixin/bean/WxXmlMessageTest.java +++ b/src/test/java/me/chanjar/weixin/bean/WxXmlMessageTest.java @@ -1,11 +1,9 @@ package me.chanjar.weixin.bean; -import me.chanjar.weixin.bean.WxXmlMessage; +import me.chanjar.weixin.api.WxConsts; import org.testng.Assert; import org.testng.annotations.Test; -import me.chanjar.weixin.api.WxConsts; - @Test public class WxXmlMessageTest {