Can a relative path be used in a Spring bean definition?

Is there a way to use the relative path, say, relative to the class path or / META -INF in the Spring bean file ? This is a little different than using ServletContextto get such information.

For example: I am trying to determine the file name for the embedded H2 database.

<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="" />

~/mydbnot so desirable because it depends on how and where you deploy the application, the home directory may not be there ... how can I make it write, for example /WEB-INF/dbstore/,?

BTW - I tried "classpath:" as suggested, in this case it does not work.

+3
source share
1 answer

The following resource prefixes are always valid:

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, " FileSystemResource" .

: Spring a > 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 URL JDBC, 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-, DB):

p:url="${dbpath}"
+6

All Articles