springspring-jdbcapplicationcontextembedded-databasefilereference

Spring Relative Path File Resource In Application Context


I have a quick question about getting spring to read a file from the application context that is external to the project. I have two projects; 1) Project A 2) Project Database. Project A has the following structure;

ProjectA
|-src
   |-main
      |-webapp
      |-WEB-INF
         |-config
         |-spring

Project Database has the following structure:

Project Database
  |-db
     |-scripts
        |-deltas

In the spring directory of Project A I have a dao-context file. In this I have configured an embedded database, however I want to initialize the database with scripts from project database. Specifically in from directory deltas.

I know the embedded database config should like the following:

<jdbc:embedded-database id="dataSource" type="H2">
    <jdbc:script location="someSchema.sql"/>
</jdbc:embedded-database>

However I am trying to determine the best way to reference the scripts in Project Database. In other words would should be the value of location if I want to use scripts in locations. A few details:

Any ideas?

Cheers

EDIT: Both projects are just folders and not jars. Neither projects are in each others classpath. Other than the fact that they are in the same directory there is no relation.

EDIT 2: Another scenario (and likely to happen) is that the db scripts could be in the same project as ProjectA , but not in the classpath. For example the directory structure could look like this:

ProjectA
|-db
   |-scripts
      |-deltas
|-src
   |-main
      |-webapp
      |-WEB-INF
         |-config
         |-spring

If this was the case how would I access the scripts in the 'deltas' directory from the 'spring' directory?


Solution

  • I thing I have (more or less) understood what your asked, but there are many caveats.

    First, when developping apps, there are 3 categories of paths :

    A web application is normally executed independantly of its source path, eventually on another machine : you build the war and deploy in on a servlet or JEE container.

    What you can do is use an environment variable, a system property or a property value to set the root of a data path in the web application. Spring is kind enough to allow environment variable to override property values when using a PropertySourcesPlaceholderConfigurer to set ${...} values in an application context. Then you use that value to get access to your datas outside of the project.

    In your example, you define such a property for the db location, and access all your database configuration relatively to that location.