Why does JAXBContext need to be told specifically about a class that is already in the package?

后端 未结 2 842
遥遥无期
遥遥无期 2020-12-17 06:36

This program:

import foo.bar.baz.ClassSpecificallyIncluded;  
import javax.xml.bind.JAXBContext;  
public class A {  
    public static void main(String[]          


        
2条回答
  •  时光取名叫无心
    2020-12-17 07:00

    The answer to your question is effectively answered here:

    Can you find all classes in a package using reflection?

    Answer: No By design, java doesn't know and can't find every class in a package. Java is built on the concept of "Just in time". Which for this purpose means that java doesn't know what it's got until it's asked for something specifically.

    So from that stand point, JAXB has no way to find what classes are in a package.

    It would be inconvenient if JAXB had to be told precisely every class every time you needed context. So as a convenience method JAXB offers you the option of providing a package name.

    In order to get round the limitations of java it attempts to load from that package an ObjectFactory and a jaxb.index file. It can do this because they are specific names. If one is found, then it uses them as a manifest for the package. If neither is found it has no option but to abort because it can't possibly know what's in the package.

提交回复
热议问题