PDA

View Full Version : Want to load object definition file dynamically


Darthy
04-12-2007, 10:00 AM
Hello,

First I want to say I'm fairly new to this so bear with me :)

I would like to load my object definition file dynamically, depending on a command line parameter.

Let's say I have a couple of config files which hold my object definition, and when booting my program I want to load the appropriate config file. I tried the following:

IApplicationContext ctx = new XmlApplicationContext("file://" + args[0] + "-spring.xml");
Runner runner = (Runner)ctx.GetObject("ExportRunnerFacade");

As a command line parameter I pass "test". My file is as follows (well made a bit smaller for this purpose):

<?xml version="1.0" encoding="utf-8" ?>
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<description>Export tool</description>
<!--!FACADE!-->
<object id="ExportRunnerFacade"
type="Switcher.Facade.Export.Runner, Switcher.Facade">
<property name="ExportFacade" ref="ExportFacade.Test"/>
</object>
....
</objects>
</spring>

I get the following error:
Unhandled Exception: Spring.Objects.Factory.ObjectDefinitionStoreExcept ion: Error registering object with name '' defined in 'file 'C:\Projects\SwitcherSolution
\Switcher.Export\bin\Debug\test-spring.xml]' : There is no parser registered for namespace ''

I know there's probably something wrong with the <resource uri="config://spring/objects"/> tag, but I don't know how to make this dynamically, even with a file:// instead.

My applications is a sort of universal export tool, which injects certain classes. I want to have 1 version of the tool deployed, and then different spring config files where i can inject implementations (like csv, xml exporters etc). It all works fine, but I need to be able to set my spring config at runtime, else I'm going to have to deploy the tool several times each with another config file which seems silly to me.

I saw someone else on the forum with the same error message but he managed to fix it by adding :
xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" but as you can see I did that.

Also for information purposes here's my app.config snippet

<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>




Can anyone help ?

Thanks

Erich Eichinger
04-12-2007, 10:11 AM
Hi,

You can't load your App.config into an XmlApplicationContext. Change the contents of your "test-spring.xml" file to


<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
<description>Export tool</description>
<!--!FACADE!-->
<object id="ExportRunnerFacade"
type="Switcher.Facade.Export.Runner, Switcher.Facade">
<property name="ExportFacade" ref="ExportFacade.Test"/>
</object>
....
</objects>


and you should be done.

You are mixing up App.config configuration and the spring-objects schema. Please have a look at the reference documentation (http://www.springframework.net/doc-latest/reference/html/objects.html#objects-factory) on configuring an ApplicationContext.

cheers,
Erich

Darthy
04-12-2007, 10:18 AM
Thanks this works ! :)

I was looking for an example on a spring config outside of the app.config to see what needs to be in there and what not, but couldn't really find one. I guess I didn't look hard enough :) I'll take a look at the link you posted, looks like that one answers most of my questions !

Thanks again,

Jorn