Spring MVC中的类路径

时间:2012-01-02 14:45:35

标签: java spring java-ee spring-mvc

当我在spring XML文件中使用 class-path属性时,如下所示

<bean class="com.fritolay.suppliernet.common.lastresorthandler.impl.SingletonPropertiesRepository">
<property name="propertiesFile" value="classpath:ecr/sample.properties" />
</bean>

在这种情况下,服务器在哪里查找应用程序中的属性文件?

我的问题是我有两个模块[1.Web Project 2.Java Project]。我在web项目中定义这个XML文件,并将属性文件放在java project src文件夹下。但是当我部署我的应用程序时,我收到以下错误:

PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'propertiesFile' threw exception; nested exception is java.io.FileNotFoundException: class path resource [ecr/sample.properties] cannot be resolved to absolute file path because it does not reside in the file system: zip:C:/Oracle/Middleware/user_projects/domains/sample/servers/AdminServer/tmp/_WL_user/_appsdir_supplier-net_ear/6e86rt/sample-service.jar!/ecr/sample.properties

2 个答案:

答案 0 :(得分:2)

如果您有两个不同的项目,则需要确保在部署 Web项目时,可以访问 java项目中的代码。如果您以分解的方式工作,则需要从 Web项目访问 java项目,在eclipse中很容易做到。

答案 1 :(得分:1)

您的Java引擎正在此位置查找您的文件(请仔细阅读):

C:/Oracle/Middleware/user_projects/domains/sample/servers/AdminServer/tmp/_WL_user/_appsdir_supplier-net_ear/6e86rt/sample-service.jar!/ecr/sample.properties

正如您所看到的,!标记意味着它正在临时JAR文件中查找您的文件(当然,这是一个IDE工件)。

您的IDE(或您选择用于部署的工具)似乎未在JAR中包含您的属性文件,从而导致错误。 可能是因为:

  • 在编译时,您的工具没有将这些资源复制到 编译目标目录(usualy /classes
  • 在打包时,您的JAR构建工具不包括 将properties资源放入Jar中,避免使用它们。

在这两种情况下,要修复它,您必须检查构建和部署配置,具体取决于您使用的IDE或工具。