`
happmaoo
  • 浏览: 4593669 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

“中文问题没商量”之Spring2.0项目中的Bug一例

阅读更多
<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
  Spring是一个非常优秀的开源项目,然而,跟其它任何优秀的系统产品一样,也存在着这样那样的问题,我们喜欢称为Bug。Spring中的Bug确实不少,今天为了充实“中文问题没商量”主题,举一个不算很重要,也比较简单理解的一个Bug示例。
  这里提前申明,这个话题不是针对Spring项目,因此请“春迷”们自重、没事勿扰,文中不足之处欢迎大家批评指教。
  我们知道,一个开源软件项目,给用户的单元测试最基本的要求是能全部通过测试,在Java中就是在运行单元测试的时候应该要看见一个绿条。Spring项目的单元测试写得非常好,也非常全面。然而,单元测试中却有一些问题,在中文路径上无法完全通过测试,必须放到英文路径下才能完全通过测试,因此,这属于一种“没商量”的中文问题。
  单元测试:
  包:org.springframework.beans.factory.xml
  类:XmlBeanFactoryTests
  方法:testFileSystemResourceWithImport
  错误图示:


  详细错误信息:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [C:\Documents%20and%20Settings\Administrator\%e6%a1%8c%e9%9d%a2\spring\spring-framework-2.0-rc2\bin\org\springframework\beans\factory\xml\resource.xml]; nested exception is java.io.FileNotFoundException: C:\Documents%20and%20Settings\Administrator\%e6%a1%8c%e9%9d%a2\spring\spring-framework-2.0-rc2\bin\org\springframework\beans\factory\xml\resource.xml (系统找不到指定的路径。)
Caused by: java.io.FileNotFoundException: C:\Documents%20and%20Settings\Administrator\%e6%a1%8c%e9%9d%a2\spring\spring-framework-2.0-rc2\bin\org\springframework\beans\factory\xml\resource.xml (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)<br>at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:85)<br>at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)<br>at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:315)<br>at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)<br>at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)<br>at org.springframework.beans.factory.xml.XmlBeanFactoryTests.testFileSystemResourceWithImport(XmlBeanFactoryTests.java:946)<br>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br>at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)<br>at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)<br>at java.lang.reflect.Method.invoke(Method.java:585)<br>at junit.framework.TestCase.runTest(TestCase.java:154)<br>at junit.framework.TestCase.runBare(TestCase.java:127)<br>at junit.framework.TestResult$1.protect(TestResult.java:106)<br>at junit.framework.TestResult.runProtected(TestResult.java:124)<br>at junit.framework.TestResult.run(TestResult.java:109)<br>at junit.framework.TestCase.run(TestCase.java:118)<br>at junit.framework.TestSuite.runTest(TestSuite.java:208)<br>at junit.framework.TestSuite.run(TestSuite.java:203)<br>at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)<br>at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)<br>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)<br>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)<br>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)<br>at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)</init></init></init>

测试代码:
public void testFileSystemResourceWithImport() {
String file = getClass().getResource("resource.xml").getFile();
XmlBeanFactory xbf = new XmlBeanFactory(new FileSystemResource(file));
// comes from "resourceImport.xml"
ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1");
// comes from "resource.xml"
ResourceTestBean resource2 = (ResourceTestBean) xbf.getBean("resource2");
}
出错原因:
  这个问题是笔者在参与开发EasyJWeb及EasyDBO框架中遇到过的问题,因此很容易就找到了问题的所在。Java的Class.getResource(name)返回的是一个URL,而URL.getFile默认情况下返回的是经过URL编码后的字符,会把中文等特殊字符变成类似%e6的形式。而一般io构造路径是没有自动解码功能的,所以在中文路径下要出现错误。
解决办法:
  在使用URL.getFile返回的路径时,使用前需要使用java.net.URLDecoder对路径进行一次解码操作。修改后的且能通过测试的方法如下:
public void testFileSystemResourceWithImport() {
String file = getClass().getResource("resource.xml").getFile();
try{
file=java.net.URLDecoder.decode(file,"UTF-8");
}
catch(Exception e)
{
e.printStackTrace();
}
XmlBeanFactory xbf = new XmlBeanFactory(new FileSystemResource(file));
// comes from "resourceImport.xml"
ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1");
// comes from "resource.xml"
ResourceTestBean resource2 = (ResourceTestBean) xbf.getBean("resource2");
}
小结:
  由于Spring开源项目的开发团队中,除了一些喜欢跟在Rod大叔的屁股后面唱中文版赞歌的“春迷”以外,当前似乎还没有中国人参与到正式的Spring开发小组中。因此,也许没有在中文路径下运行过测试用例,导致我这样的Spring初学者一不小心就遇上这样的问题。
  问题是解决了,但是却是一种比较罕见的方式,而且Spring开发小组事先也许没有预想到或是故意忽略掉的问题,因此,可以称得上是“没商量”的中文问题。
  (注:本文作者,EasyJF开源团队 大峡,转载请保留作者声明!)


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1099389


分享到:
评论

相关推荐

    SPRING2.0中文文档

    Spring 2.0 是一个里程碑式的版本,它在Java企业级开发中扮演着核心角色,为开发者提供了丰富的功能和灵活性。这份全中文的Spring 2.0技术文档是学习和理解这一版本的重要参考资料,旨在帮助中国开发者更好地掌握...

    Spring 2.0 中文参考手册

    Spring 2.0 中文参考手册,Spring 2.0 中文参考手册,Spring 2.0 中文参考手册,Spring 2.0 中文参考手册Spring 2.0 中文参考手册,

    spring2.0中文手册及使用指南 chm

    Spring 2.0 是一个非常重要的Java框架,它在企业级应用开发中占据了核心地位,尤其是在基于Java的轻量级应用程序上下文(IoC)和面向切面编程(AOP)方面。本手册和使用指南提供了全面的Spring 2.0相关知识,包括其...

    Spring2.0中文教程

    Spring 2.0是Spring框架的一个重要版本,它在Java企业级应用开发中扮演着核心角色。本教程将深入探讨Spring 2.0的核心特性和关键功能,旨在帮助开发者掌握这个强大框架的基础与高级概念。 1. **依赖注入...

    spring2.0中文参考手册.rar

    《Spring 2.0 中文参考手册》是一个详尽阐述 Spring 框架核心特性和扩展功能的重要资源,尤其对于中文开发者来说,它提供了一条理解 Spring 2.0 版本的关键途径。Spring 是一个广泛应用的 Java 开发框架,以其模块化...

    spring2.0 中文教程

    通过阅读《Spring 2.0中文教程》中的内容,开发者可以深入理解这些概念,并学会如何在实际项目中运用Spring框架。这本书将帮助读者掌握Spring的核心功能,提高开发效率,打造更加健壮的企业级应用。

    spring 2.0中文参考手册

    《Spring 2.0中文参考手册》是一本深入解析Spring框架的重要资料,它全面覆盖了Spring 2.0版本的核心特性和应用场景。Spring作为一个轻量级的Java企业级应用框架,以其模块化、松耦合的设计理念,已经成为现代Java...

    spring2.0 中文帮助文档 pdf

    spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档 spring2.0 中文帮助文档

    Spring 2.0中文API(chm格式)

    Spring框架是Java应用程序开发中的一个核心组件,尤其在企业级应用中广泛应用。Spring 2.0版本是一个重要的里程碑,引入了许多关键特性和改进,为开发者提供了更强大的工具和灵活性。这个压缩包包含的是Spring 2.0的...

    spring2.0中文开发参考手册(CHM)

    《Spring 2.0中文开发参考手册》是针对Spring框架2.0版本的一份详尽指南,旨在帮助开发者深入理解并充分利用这一版本的新特性和改进。Spring作为一个强大的Java企业级应用框架,它提供了诸如依赖注入、面向切面编程...

    Spring2.0宝典源代码

    通过《Spring2.0宝典》的源代码,学习者可以逐步探索上述功能的实际运用,了解每个特性在项目中的实现方式,从而提升对Spring框架的理解和使用能力。在阅读源代码的过程中,建议结合书中的讲解,按照章节顺序逐步...

    spring2.0jar包(一)

    spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一) spring2.0jar包(一)

    spring2.0(中文)

    它通过实际完成一个完整的Spring项目示例,展示了与Spring相关API的使用技巧,能够显著减少每一位入门者摸索SpringAPI的时间。本书是学习SpringWeb开发的最佳读物,它能让读者在示例学习中获得显著提高。

    spring2.0完整开发包

    在实际开发中,使用Eclipse作为IDE,Spring 2.0的开发包通常包含Spring的库文件、源码、文档以及可能的Eclipse插件,这些资源帮助开发者快速设置项目环境,理解和使用Spring的各种功能。 总结来说,Spring 2.0是...

    spring 2.0中文参考文档

    Spring 2.0中文参考文档是一份详尽的指南,旨在帮助开发者深入理解Spring框架的各个方面。这份文档包含了丰富的信息,涵盖了从基本概念到高级特性的所有内容,为使用Spring 2.0版本进行开发提供了全面指导。 1. **...

    spring2.0 jar包

    Spring 2.0是Spring框架的一个重要版本,它在2006年发布,标志着Spring框架的显著进步和发展。这个版本引入了许多新特性,优化了已有功能,并为开发者提供了更强大的工具来构建企业级Java应用。以下是Spring 2.0中的...

    Spring 2.0 spring 2.0 标准API

    Spring 2.0 标准API 用处不大的资源我不发

Global site tag (gtag.js) - Google Analytics