首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JAXB学习 (4)JAXBContext

2013-08-04 
JAXB学习 (四)JAXBContext可以看到,它一直调到了javax.xml.bind.ContextFinder类的find()方法ContextFinde

JAXB学习 (四)JAXBContext

可以看到,它一直调到了javax.xml.bind.ContextFinder类的find()方法

    ContextFinder.find(                              /* The default property name according to the JAXB spec */                              JAXB_CONTEXT_FACTORY,                                    /* the context path supplied by the client app */                              contextPath,                                    /* class loader to be used */                              classLoader,                              properties );  
    javax.xml.bind.context.factory=com.liulutu.student.model.CustomJAXBContextFactory
      package com.liulutu.student.model; import javax.xml.bind.JAXBContext; public class CustomJAXBContextFactory{ }
        JAXBContext jaxbContext = JAXBContext.newInstance("com.liulutu.student.model"); ObjectFactory factory = new ObjectFactory(); Students students = factory.createStudents(); addNewStudentTo(factory, students, "a", SexType.MALE); addNewStudentTo(factory, students, "b", SexType.FEMALE); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setSchema(SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( new File("students.xsd"))); marshaller.marshal(students, new File("a.xml"));

        ?

        ?然后检查一下运行结果:

        ?

        Exception in thread "main" javax.xml.bind.JAXBException: Provider com.liulutu.student.model.CustomJAXBContextFactory could not be instantiated: java.lang.NoSuchMethodException: com.liulutu.student.model.CustomJAXBContextFactory.createContext(java.lang.String, java.lang.ClassLoader) - with linked exception: [java.lang.NoSuchMethodException: com.liulutu.student.model.CustomJAXBContextFactory.createContext(java.lang.String, java.lang.ClassLoader)] at javax.xml.bind.ContextFinder.newInstance(Unknown Source) at javax.xml.bind.ContextFinder.find(Unknown Source) at javax.xml.bind.JAXBContext.newInstance(Unknown Source) at javax.xml.bind.JAXBContext.newInstance(Unknown Source) at javax.xml.bind.JAXBContext.newInstance(Unknown Source) at com.liulutu.student.test.TestMarshaller.main(TestMarshaller.java:22) Caused by: java.lang.NoSuchMethodException: com.liulutu.student.model.CustomJAXBContextFactory.createContext(java.lang.String, java.lang.ClassLoader) at java.lang.Class.getMethod(Unknown Source) ... 6 more
          ?

        ?

        ?

热点排行