简体   繁体   English

Spring-类路径资源中的相对路径

[英]Spring - relative path in classpath resource

I'm wondering if if (and how) I could use relative classpath resource paths within spring. 我想知道是否(以及如何)在Spring内使用相对的类路径资源路径。 I've following application structure: 我遵循以下应用程序结构:

src/
  org/me/
    mainContext.xml
    app1/
      app1Context.xml
    app2/
      app2Context.xml
      comp1/
        comp1Context.xml

Now in mainContext.xml I'd like to import bean definitions by relative paths: 现在在mainContext.xml我想通过相对路径导入bean定义:

<import resource="classpath:app1/app1Context.xml"/>
<import resource="classpath:app2/app2Context.xml"/>

and similary in app2Context.xml : app2Context.xml中的app2Context.xml

<import resource="classpath:comp1/comp1Context.xml"/>

(The org.me.app2 code is in different jar ( WEB-INF/lib/app2.jar )). org.me.app2代码位于其他jar( WEB-INF/lib/app2.jar )中)。

However currently I'm getting FileNotFoundException exception: 但是目前我正在得到FileNotFoundException异常:

java.io.FileNotFoundException: class path resource [app1/app1Context.xml] cannot be opened because it does not exist

Can I use such relative paths? 我可以使用这样的相对路径吗? How? 怎么样? To which path are relative classpath paths resolved? 相对的类路径路径解析到哪个路径?

There is a similar question ( Relative paths in spring classpath resource ) but people are only suggesting what I'm already doing (and what doesn't work right now). 有一个类似的问题( spring classpath资源中的相对路径 ),但是人们只是在暗示我已经在做的事情(以及什么现在不起作用)。

There is a similar question ( Relative paths in spring classpath resource ) but people are only suggesting what I'm already doing (and what doesn't work right now). 有一个类似的问题( spring classpath资源中的相对路径 ),但是人们只是在暗示我已经在做的事情(以及什么现在不起作用)。

That is wrong: The people answered with paths that are relative to the classpath root, but not relative to the location of the xml! 这是错误的:人们回答的路径是相对于类路径根目录,而不是相对于xml的位置! (*You should notice that the example in " Relative paths in spring classpath resource " is a maven project, so the folder resources become the classpath root) (*您应注意,“ spring classpath资源中的相对路径 ”中的示例是一个maven项目,因此文件夹资源将成为classpath的根目录)

So if you want to use the same technique then the imports would be (with org/me ): 因此,如果您想使用相同的技术,则导入将是(使用org/me ):

<import resource="classpath:org/me/app1/app1Context.xml"/>
<import resource="classpath:org/me/app2/app2Context.xml"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM