Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ronald Castillo
Ranch Hand
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ronald Castillo
Ranch Hand
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic