meatballli 2008-12-22 17:34
浏览 244
已采纳

请问Spring如何在jar文件里面按文件夹加载配置文件?

一个Web应用有多个模块(假设有org和auth两个模块), 我希望为每个模块创建一个项目, 在项目中维护模块用到的配置文件. 然后将这些模块分别打包成jar放到web应用的WEB-INF/lib下.

现在用单元测试, 在Web应用中运行单元测试, 如果在Web应用的Build Path/Project中添加模块项目, 单元测试能够成功, 如果使用Build Path/Libraries添加模块jar文件, 运行单元测试失败. Spring中加载配置文件代码如下:
[code="xml"]
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">





classpath*:/config/hibernate/app/
classpath*:/config/hibernate/framework/


...

[/code]

每个jar包里面都有/config/hibernate/framework文件夹

错误信息: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'menuService' defined in URL [jar:file:/E:/home/workspaces/eclipse/simple-web/WebContent/WEB-INF/lib/auth.jar!/config/spring/app/services.xml]: Cannot resolve reference to bean 'defaultMediator' while setting bean property 'defautMediator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultMediator' defined in class path resource [config/env/base-dao.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [config/persistence/hibernate.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: URL [jar:file:/E:/home/workspaces/eclipse/simple-web/WebContent/WEB-INF/lib/auth.jar!/config/hibernate/framework/] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/E:/home/workspaces/eclipse/simple-web/WebContent/WEB-INF/lib/auth.jar!/config/hibernate/framework/

网上找到一个相关的讨论: http://forum.springframework.org/archive/index.php/t-10029.html
好像是说对于directory的加载必须是文件夹必须存在于文件系统中, jar下面的文件夹找不到.不知道这个问题有没有办法解决?

谢谢先!

ps. 第一次提问, 如果有不当之处请各位包涵
[b]问题补充:[/b]
感谢czlonly的回答, 不过能不能写详细一点?
我刚才试了一下, 如果把配置文件改成
[code="xml"]
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">





classpath*:/config/hibernate/framework/*.xml


...

[/code]

果然可以了

  • 写回答

1条回答 默认 最新

  • adday123 2008-12-22 21:27
    关注

    classpath*:config/hibernate/framework/*.xml

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿