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

发布于 2024-10-24 08:06:00 字数 554 浏览 4 评论 0原文

有没有办法使用相对路径,比如相对于Spring bean定义文件中的类路径或/META-INF?这与使用 ServletContext 获取此类信息有点不同。

例如:我正在尝试为嵌入式数据库 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 并不是那么理想,因为它取决于您部署应用程序的方式和位置,主目录可能不存在......我怎样才能使其写入,例如,/WEB-INF/dbstore/

顺便说一句 - 我按照建议尝试了“classpath:”,在这种情况下它似乎不起作用。

Is there a way to use relative path, say relative to class path or /META-INF in Spring bean definition file? This is a bit different from using ServletContext to obtain such info.

For example: I am trying to define a filename for a embedded database 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/?

BTW - I tried "classpath:" as suggested, it doesn't seem to work in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-10-31 08:06:01

以下资源前缀始终有效:

表 4.1。资源字符串

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] 但另请参阅第 4.7.3 节,
“文件系统资源注意事项”

来源: Spring 参考> ResourceLoader

但我真的不明白相对路径如何适合其中。也许您应该详细说明您的要求。


感谢您提供额外信息。你是对的,它不能在这种情况下工作

<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 从不分析 JDBC URL,它只是将其传递给 bean。我建议是使用 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"  />

现在您可以在类路径或每个系统属性的属性文件中配置路径。实际上,您可能想做这样的事情(使整个 URL 可配置,而不仅仅是数据库模式名称):

p:url="${dbpath}"

The following resource prefixes are always valid:

Table 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”
.

Source: Spring Reference > The 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. What I'd suggest is to use the PropertyPlaceHolderConfigurer mechanism:

<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):

p:url="${dbpath}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文