我可以在 Spring bean 定义中使用相对路径吗?
有没有办法使用相对路径,比如相对于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下资源前缀始终有效:
表 4.1。资源字符串
来源: Spring 参考>
ResourceLoader
但我真的不明白相对路径如何适合其中。也许您应该详细说明您的要求。
感谢您提供额外信息。你是对的,它不能在这种情况下工作
Spring 从不分析 JDBC URL,它只是将其传递给 bean。我建议是使用
PropertyPlaceHolderConfigurer
机制:现在您可以在类路径或每个系统属性的属性文件中配置路径。实际上,您可能想做这样的事情(使整个 URL 可配置,而不仅仅是数据库模式名称):
The following resource prefixes are always valid:
Table 4.1. Resource strings
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
Spring never analyzes that JDBC URL, it just passes it to the bean. What I'd suggest is to use the
PropertyPlaceHolderConfigurer
mechanism: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):