简体   繁体   English

我可以在Spring bean定义中使用相对路径吗?

[英]Can I use relative path in Spring bean definition?

Is there a way to use relative path, say relative to class path or /META-INF in Spring bean definition file ? 有没有办法在Spring bean定义文件中使用相对路径,比如相对于类路径或/ META-INF? This is a bit different from using ServletContext to obtain such info. 这与使用ServletContext获取此类信息略有不同。

For example: I am trying to define a filename for a embedded database H2. 例如:我正在尝试为嵌入式数据库H2定义文件名。

<bean id="myDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="org.h2.Driver"
    p:url="jdbc:h2:~/mydb;AUTO_SERVER=TRUE"
    p:username=""
    p:password="" />

~/mydb is not so desired as it depends on how and where you deploy the application, the home directory may not be there ... how can I make it to write to, for example, /WEB-INF/dbstore/ ? ~/mydb不是那么需要,因为它取决于你部署应用程序的方式和位置,主目录可能不在那里......我怎样才能让它写入,例如, /WEB-INF/dbstore/

BTW - I tried "classpath:" as suggested, it doesn't seem to work in this case. 顺便说一句 - 我按照建议尝试了“classpath:”,它在这种情况下似乎不起作用。

The following resource prefixes are always valid: 以下资源前缀始终有效:

Table 4.1. 表4.1。 Resource strings 资源字符串

Prefix       Example                            Explanation
---------------------------------------------------------------------------
classpath: | classpath:com/myapp/config.xml  |  Loaded from the classpath.
file:      | file:/data/config.xml           |  Loaded as a URL, from the
           |                                 |  filesystem. [1]
http:      | http://myserver/logo.png        |  Loaded as a URL.
(none)     | /data/config.xml                |  Depends on the underlying
           |                                 |  ApplicationContext.

[1] But see also Section 4.7.3, “FileSystemResource caveats” . [1]但另见第4.7.3节“FileSystemResource告诫”

Source: Spring Reference > The ResourceLoader 来源: Spring Reference> ResourceLoader

But I don't really see how relative paths fit in there. 但我真的没有看到相对路径如何适应那里。 Perhaps you should elaborate your requirements. 也许你应该详细说明你的要求。


Thanks for the additional info. 感谢您提供更多信息。 You're right, it can't work in this context 你是对的,它不能在这种情况下工作

<bean id="myDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="org.h2.Driver"
    p:url="jdbc:h2:~/mydb;AUTO_SERVER=TRUE"
    p:username=""
    p:password="" />

Spring never analyzes that JDBC URL, it just passes it to the bean. Spring从不分析JDBC URL,它只是将它传递给bean。 What I'd suggest is to use the PropertyPlaceHolderConfigurer mechanism : 我建议使用PropertyPlaceHolderConfigurer机制

<bean id="myDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="org.h2.Driver"
    p:url="jdbc:h2:${dbpath};AUTO_SERVER=TRUE"
    p:username=""
    p:password="" />

<!-- example config -->
<context:property-placeholder location="classpath:com/foo/jdbc.properties"
                              systemPropertiesMode="override"  />

Now you can either configure the path in a properties file on the classpath or per system property. 现在,您可以在类路径或每个系统属性的属性文件中配置路径。 Actually, you probably want to do something like this (make the entire URL configurable, not just the DB schema name): 实际上,您可能希望执行以下操作(使整个URL可配置,而不仅仅是数据库模式名称):

p:url="${dbpath}"

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

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