File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring config files inside jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring config files inside jar" Watch "Spring config files inside jar" New topic
Author

Spring config files inside jar

Manoj Raghuwanshi
Ranch Hand

Joined: Jun 20, 2004
Posts: 75
In my application I am using spring configuration files from two locations , first inside a jar file and second in the classes directory. Both configuration files have the same name – beanRefContext.xml. I am not supposed to change the name of these files. I just want to know what will happen to the beans defined in these files? How these beans get initialized?

Thanks in advance

~Manoj
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
Manoj Raghuwanshi wrote:In my application I am using spring configuration files from two locations , first inside a jar file and second in the classes directory. Both configuration files have the same name – beanRefContext.xml. I am not supposed to change the name of these files. I just want to know what will happen to the beans defined in these files? How these beans get initialized?

I think it depends on which ApplicationContext implementation you use. If you are talking about a basic Spring Java app, you will probably be using ClassPathXmlApplicationContext. If that's the case, I suspect both versions of the file will get read, which doesn't seem like a good idea. I assume the contents of the beanRefContext.xml file are the same?? I haven't tried this but two possible outcomes occur to me.
1. You may get a duplicate definition warning and end up with only one instance of your defined beans.
2. You may get a critical exception for duplicate definitions.

Ways you might avoid this...

1. Is the effective classpath the same for both files? I.e. could you essentially ensure that each is in their own package and only reference one of them with the full package path in your application?
2. Is this a web-mvc application? If so, you may be able to explicitly specify which file gets loaded by the init params for the ContextLoaderListener

Best of luck,

Mark
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
Actually... I just found this reference in the Spring JavaDocs for ClassPathXmlApplicationContext

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/support/ClassPathXmlApplicationContext.html

Note: In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

I take this to mean that if your files are the same... no big deal. If you've modified your local version and it's loaded last, then that definition wins.

Mark
Manoj Raghuwanshi
Ranch Hand

Joined: Jun 20, 2004
Posts: 75
Thanks Mark for the detailed information. But my problem is bit complicated. Actaully I dig more in Spring and found that the application uses Contextsingletonbeanfactorylocator to load the spring context. This class will load the application context only once and will return the same context if try to load it again. I am using weblogic and jdk5. I have the beanRefContext.xml(I learn today that this should be the default name of config file) located in App-Inf\classes.


I have a jar in the App-Inf\lib -audit.jar which also contains beanRefContext.jar. It contents below definations:-



If you observe carefully , it has omitted abc.xml.
But my application uses spring beans defined in abc.xml. When weblogic load the context - prod.beanfactory it loads it from this jar file located in App-Inf\lib. Is there any way I can tell weblogic that load beanRefContext.xml from App-Inf\classes instead of App-Inf\lib?

Thanks for yout time.

Regards,
Manoj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring config files inside jar