From 0abadf092936f2545ac9e9543289a27c60b51514 Mon Sep 17 00:00:00 2001 From: haogrgr Date: Wed, 15 Oct 2014 14:07:55 +0800 Subject: [PATCH] =?UTF-8?q?JAXBContext=E8=87=AA=E8=BA=AB=E6=98=AF=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E5=AE=89=E5=85=A8=E7=9A=84,=E8=80=8C=E8=AF=A5?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E5=88=9B=E5=BB=BA=E6=AF=94=E8=BE=83=E8=80=97?= =?UTF-8?q?=E6=97=B6,=E5=BE=AE=E4=BF=A1=E6=B6=88=E6=81=AF=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E9=9D=9E?= =?UTF-8?q?=E5=B8=B8=E9=A2=91=E7=B9=81,=E7=BC=93=E5=AD=98JAXBContext?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=8F=90=E9=AB=98=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/util/xml/XmlTransformer.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 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 b6fb5f2bf..49ff0516d 100644 --- a/src/main/java/me/chanjar/weixin/util/xml/XmlTransformer.java +++ b/src/main/java/me/chanjar/weixin/util/xml/XmlTransformer.java @@ -5,6 +5,8 @@ import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; +import java.util.HashMap; +import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -24,15 +26,17 @@ public class XmlTransformer { * @return * @throws JAXBException */ + @SuppressWarnings("unchecked") public static T fromXml(Class clazz, String xml) throws JAXBException { - JAXBContext context = JAXBContext.newInstance(clazz); + JAXBContext context = getJAXBContext(clazz); 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 = JAXBContext.newInstance(clazz); + JAXBContext context = getJAXBContext(clazz); Unmarshaller um = context.createUnmarshaller(); InputSource inputSource = new InputSource(is); inputSource.setEncoding("utf-8"); @@ -53,7 +57,7 @@ public class XmlTransformer { } public static void toXml(Class clazz, T object, Writer writer) throws JAXBException { - JAXBContext context = JAXBContext.newInstance(clazz); + JAXBContext context = getJAXBContext(clazz); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(CharacterEscapeHandler.class.getName(), characterUnescapeHandler); @@ -68,4 +72,20 @@ public class XmlTransformer { } } + 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; + } + }