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.
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}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.