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?
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
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.
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?