根据JAXB官方文档改进JAXBContext的使用

This commit is contained in:
Daniel Qian
2014-10-15 15:14:26 +08:00
parent 70d5ea403e
commit 457a9809db
2 changed files with 39 additions and 34 deletions

View File

@ -8,84 +8,91 @@ import java.io.Writer;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.xml.bind.JAXBContext; import javax.xml.bind.*;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import me.chanjar.weixin.bean.*;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler; import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
public class XmlTransformer { public class XmlTransformer {
protected static final JAXBContext jaxbContext = initJAXBContext();
/** /**
* xml -> pojo * xml -> pojo
*
* @param clazz * @param clazz
* @param object * @param xml
* @return * @return
* @throws JAXBException * @throws JAXBException
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T fromXml(Class<T> clazz, String xml) throws JAXBException { public static <T> T fromXml(Class<T> clazz, String xml) throws JAXBException {
JAXBContext context = getJAXBContext(clazz); JAXBContext context = jaxbContext;
Unmarshaller um = context.createUnmarshaller(); Unmarshaller um = context.createUnmarshaller();
T object = (T) um.unmarshal(new StringReader(xml)); T object = (T) um.unmarshal(new StringReader(xml));
return object; return object;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException { public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
JAXBContext context = getJAXBContext(clazz); JAXBContext context = jaxbContext;
Unmarshaller um = context.createUnmarshaller(); Unmarshaller um = context.createUnmarshaller();
InputSource inputSource = new InputSource(is); InputSource inputSource = new InputSource(is);
inputSource.setEncoding("utf-8"); inputSource.setEncoding("utf-8");
T object = (T) um.unmarshal(inputSource); T object = (T) um.unmarshal(inputSource);
return object; return object;
} }
/** /**
* pojo -> xml * pojo -> xml
*
* @param clazz * @param clazz
* @param object
* @return * @return
* @throws JAXBException * @throws JAXBException
*/ */
public static <T> String toXml(Class<T> clazz, T object) throws JAXBException { public static <T> String toXml(Class<T> clazz, T object) throws JAXBException {
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
toXml(clazz, object, stringWriter); toXml(clazz, object, stringWriter);
return stringWriter.getBuffer().toString(); return stringWriter.getBuffer().toString();
} }
public static <T> void toXml(Class<T> clazz, T object, Writer writer) throws JAXBException { public static <T> void toXml(Class<T> clazz, T object, Writer writer) throws JAXBException {
JAXBContext context = getJAXBContext(clazz); JAXBContext context = jaxbContext;
Marshaller m = context.createMarshaller(); Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(CharacterEscapeHandler.class.getName(), characterUnescapeHandler); m.setProperty(CharacterEscapeHandler.class.getName(), characterUnescapeHandler);
m.marshal(object, writer); m.marshal(object, writer);
} }
protected static CharacterEscapeHandler characterUnescapeHandler = new CharacterUnescapeHandler(); protected static CharacterEscapeHandler characterUnescapeHandler = new CharacterUnescapeHandler();
protected static class CharacterUnescapeHandler implements CharacterEscapeHandler { protected static class CharacterUnescapeHandler implements CharacterEscapeHandler {
public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException { public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException {
writer.write(ac, i, j); writer.write(ac, i, j);
} }
} }
protected static Map<Class<?>, JAXBContext> jaxbContexts = new HashMap<Class<?>, JAXBContext>(); private static JAXBContext initJAXBContext() {
/*
protected static JAXBContext getJAXBContext(Class<?> clazz){ * JAXBContext对象是线程安全的根据官方文档的建议将对象作为全局实例
//JAXBContext是线程安全的,且创建相对比较耗性能,使用map缓存,并发下多次创建没问题. * https://jaxb.java.net/guide/Performance_and_thread_safety.html
JAXBContext context = jaxbContexts.get(clazz); */
if(context == null){ try {
try { return JAXBContext.newInstance(
context = JAXBContext.newInstance(clazz); WxXmlOutMessage.class,
} catch (JAXBException e) { WxXmlOutImageMessage.class,
throw new RuntimeException("创建JAXBContext实例失败:" + clazz.getName(), e); WxXmlOutMewsMessage.class,
} WxXmlOutMusicMessage.class,
jaxbContexts.put(clazz, context); WxXmlOutTextMessage.class,
} WxXmlOutVideoMessage.class,
return context; WxXmlOutVoiceMessage.class,
WxXmlMessage.class);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
} }
} }

View File

@ -1,11 +1,9 @@
package me.chanjar.weixin.bean; package me.chanjar.weixin.bean;
import me.chanjar.weixin.bean.WxXmlMessage; import me.chanjar.weixin.api.WxConsts;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import me.chanjar.weixin.api.WxConsts;
@Test @Test
public class WxXmlMessageTest { public class WxXmlMessageTest {