日期:2014-05-16  浏览次数:20842 次

找不到org.apache.xerces.jaxp.DocumentBuilderFactoryImpl的处理方法
    最近打算复习下SPRING,顺便写个例子,结果在跑程序的时候,出现了“org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found”的报错信息,程序没有从SPRING配置文件里面读取到相应的注入BEAN,程序样例为:
   
   
        Resource res = new ClassPathResource("SPRING配置文件路径");
        BeanFactory factory = new XmlBeanFactory(res);    
   

在用Bean工厂解析配置文件时出现上述错误,在网上找到了解决的原因。DocumentBuilderFactoryImpl 类是BeanFactory 默认采用的解析xml类,解决方法是,在上面两句话之前加上一句,
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
				"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");


源代码为:
   
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
				"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
        Resource res = new ClassPathResource("SPRING配置文件路径");
        BeanFactory factory = new XmlBeanFactory(res);  

之后,获取Bean对象,测试通过。