This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes Is it possible to move the beans.xml file outside my app? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Is it possible to move the beans.xml file outside my app?" Watch "Is it possible to move the beans.xml file outside my app?" New topic
Author

Is it possible to move the beans.xml file outside my app?

Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

Hi,

I'm REALLY new to Spring and I'm trying out some examples I've seen on the web so I can get familiar with it and learn.
Now, I want to do something but I'm not sure if it's possible or if it is, how to do it.

I created a simple beans.xml file, but since I want to deploy my app in a .jar file, I want to take the beans.xml OUT of my jar file so if anyone needs to change some configuration option, they don't have to touch the app or recompile.


Thanks for the help.

PD: I'm using Eclipse as my IDE.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Yes. You have two options:
1) Put the file somewhere else and add that location to your classpath.
2) Load the beans.xml by using a hard coded path on your file system. (I wouldn't do this as it introduces a dependency to a specific location.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Make sure you are using up to date examples and tutorials. The only time I have seen someone call their Spring configuration beans.xml was way way back in the old days of Spring 1.x versions.

Spring is on 3.0.5 and it is best if you are learning from scratch to learn the latest. You will get some really bad habits if you learn old Spring now.

But yes, you can load your xml configuration files from anywhere, There are different default loading mechanisms for each implementation of ApplicationContext, and there are prefixes that you can use to override those defaults.

ClassPathXmlApplicationContext - default load from the classpath
FileSystemXmlApplicationContext - default load from the file system, typically means it isn't in your jar file
XmlWebApplicationContext - default load from your Web Application context, so the root of your war file.

classpath:
file:
http:

are the three prefixes you can use to override the default.

example

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:file1.xml", "file:c://myconfigfiles/file2.xml", "http:WEB-INF/file3.xml");

I always use the prefix even if the prefix is the default loading mechanism because someone later might change that to something else and I am still guaranteed that the config files will still load from those locations.

Mark

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

Thanks for the reply.

Regarding the tutorials I'm following, they're using Spring 3.0:
http://www.vaannila.com/spring/spring-tutorial/spring-tutorial.html

Thanks a lot the help.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Ronald Castillo wrote:Thanks for the reply.

Regarding the tutorials I'm following, they're using Spring 3.0:
http://www.vaannila.com/spring/spring-tutorial/spring-tutorial.html

Thanks a lot the help.


Cool, they are using up to date ways. It was just odd to try and find where they would create their ApplicationContext.

But yes, these files can be anywhere and you can load it from any mechanism. Just use the prefixes

http:
to look into your war file typically you would put the file in your WEB-INF directory

classpath:
to look for your file in the classpath

and

file:
to look anywhere in the file system, which means files outside of your jar/war/ear/rar/car/far/mar/par/bar/sar/tar/var/yar I think you understand what I mean.

Mark
Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

I just tried it and it works flawlessly! Thanks a lot Mark.

Regarding the dependency when using a harcoded path, what I did was get the Path from where my jar is running using the File class, so that as long as the file is in the same directory as my jar, it works just fine. When not found, I added kind of a backup beans.xml inside my jar (with default configuration options).
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Ronald Castillo wrote:I just tried it and it works flawlessly! Thanks a lot Mark.

Regarding the dependency when using a harcoded path, what I did was get the Path from where my jar is running using the File class, so that as long as the file is in the same directory as my jar, it works just fine. When not found, I added kind of a backup beans.xml inside my jar (with default configuration options).


Yes, that is a good solution. The great thing about loading "resources" the beans.xml is a resource, is that you can use relative paths and even wildcards.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to move the beans.xml file outside my app?
 
Similar Threads
Can I load classes from a specific jar file at runtime?
How to run jar with no command line?
Classpath issue: class path resource [beans.xml] cannot be opened because it does not exist
How set JNDI Name for beans.xml file externally
launching jar from script.bat