为什么 java DocumentBuilder 需要 2 个步骤才能创建,而不是 1 个步骤?
Why java DocumentBuilder need 2 steps to be created, not 1 step?
使用 DOM 的 javax.xml.parsers 我必须编写如下代码:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
我查看了它的 jdk 源代码:
public static DocumentBuilderFactory newInstance() {
return FactoryFinder.find(
/* The default property name according to the JAXP spec */
DocumentBuilderFactory.class, // "javax.xml.parsers.DocumentBuilderFactory"
/* The fallback implementation class name */
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
}
我觉得很奇怪:
工厂模式通常是有一个静态工厂,然后用factory.create生成一个"product"。有工厂是不是JAXP协议原理?
只要这个文档工厂是java sdk的一部分,为什么还要"FactoryFinder.find()"找一些类可以生成这样的工厂?
谢谢!
如果出于某种原因您需要使用自定义工厂(例如,如果您在 JDK 标准工厂中遇到错误),此方法用于允许通过配置更改工厂实现。
* Use the properties file "lib/jaxp.properties" in the JRE directory.
* This configuration file is in standard <code>java.util.Properties
* </code> format and contains the fully qualified name of the
* implementation class with the key being the system property defined
* above.
使用 DOM 的 javax.xml.parsers 我必须编写如下代码:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
我查看了它的 jdk 源代码:
public static DocumentBuilderFactory newInstance() {
return FactoryFinder.find(
/* The default property name according to the JAXP spec */
DocumentBuilderFactory.class, // "javax.xml.parsers.DocumentBuilderFactory"
/* The fallback implementation class name */
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
}
我觉得很奇怪:
工厂模式通常是有一个静态工厂,然后用factory.create生成一个"product"。有工厂是不是JAXP协议原理?
只要这个文档工厂是java sdk的一部分,为什么还要"FactoryFinder.find()"找一些类可以生成这样的工厂?
谢谢!
如果出于某种原因您需要使用自定义工厂(例如,如果您在 JDK 标准工厂中遇到错误),此方法用于允许通过配置更改工厂实现。
* Use the properties file "lib/jaxp.properties" in the JRE directory.
* This configuration file is in standard <code>java.util.Properties
* </code> format and contains the fully qualified name of the
* implementation class with the key being the system property defined
* above.