• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Spring Application Context Override/Import from Classpath and Filesystem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a spring XML (main.xml) which contains:


The JAVA code is:

The compiled JAR contains both the main.xml and an empty test-override.xml.

For some of my testing I'd like to use a different test-override.xml that should be placed somewhere in the file system (or into [app_folder]/spring), so the testers can easily modify it w/o rebuilding the app or modifying the JAR.

How can I achieve this? I tried manipulating the classpath (by using -cp) but my app still using the empty XML from the JAR.

Thanks for the help.

 
author & internet detective
Posts: 41502
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
I'd welcome you to CodeRanch, but you've been a member a long time. So congrats on your first post!

The crux of the problem is that "classpath:/spring/main.xml" and "classpath:/spring/test-override.xml" are hardcoded. For the main.xml, you are already instantiating in Java code. You can change the Java code to get the value from a property file, system property, etc and point to any file you want. For the test-override one, you can still change the xml to refer to a value that comes from a property file. If you host that property file outside your jar, you make it easy to change.

I was tempted to say that ccould create an @Configuration class.

But this doesn't work because you'd need to know the name of the resource at compile time since it is in an annotation.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can use the import resource to import any file specified by a Java System Property. So if you have something like:

and you pass -Doverride="/config/my_override.xml" to java it should be imported by Spring.

Also you can try to use Spring Profile if you're on Spring v3.1 or newer.

Regards,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic