This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
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.)
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.
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.
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).
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Is it possible to move the beans.xml file outside my app?