<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Vidar's Musings &#187; spring</title>
	<atom:link href="http://www.kongsli.net/nblog/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kongsli.net/nblog</link>
	<description>Deep thoughts on shallow topics</description>
	<lastBuildDate>Fri, 27 Jan 2012 07:36:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Spring.NET: programatically add objects to the existing (XML) application context</title>
		<link>http://www.kongsli.net/nblog/2008/08/07/springnet-programatically-add-objects-to-the-existing-xml-application-context/</link>
		<comments>http://www.kongsli.net/nblog/2008/08/07/springnet-programatically-add-objects-to-the-existing-xml-application-context/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 13:10:01 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring.net]]></category>
		<category><![CDATA[wiring]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=65</guid>
		<description><![CDATA[My experience is that Spring.NET configuration files tend to grow very large. As far as I can figure, there are two principal problems that arise from this: The configuration files get difficult to read and maintain It gets easier to introduce errors in the configuration because of its size In general, I am in favour [...]]]></description>
			<content:encoded><![CDATA[<p>My experience is that Spring.NET configuration files tend to grow very large. As far as I can figure, there are two principal problems that arise from this:</p>

<ol>
    <li>The configuration files get difficult to read and maintain</li>
    <li>It gets easier to introduce errors in the configuration because of its size</li>
</ol>

<p>In general, I am in favour of keeping configuration files as small as possible. I often work with web applications that can (quite) easily be redeployed to the production environment, hence I always ask the question &#8220;will this value ever change between environments or deployments&#8221; when considering introducing a new configuration part.</p>

<p>Now, the Spring XML configuration usually serves two main purposes; to wire together the application, and to provide values that should be possible to change between deployments of the application or for different environments. The first purpose, I would argue does not necessarily need to be in the XML configuration. Rather, if this is done in code, we get the benefit that the compiler will tell us right away if there are typos or missing references. If this wiring is in the XML configuration file, such errors will not surface until the application starts.</p>

<p>So, the question that I had, was how Spring context wiring could be combined in code and in XML. I found one way of doing it, but it is only applicable to singleton objects.</p>

<p>Say, for instance that we have an object &#8220;something&#8221; that we wish to have configured in XML:
<pre>  &lt;object id="something" type="SpringTest.Something, SpringTest" singleton="false"/&gt;
</pre>
Then, we have a class that we want to initialize in code:
<pre>class Foo</pre>
<pre>{</pre>
<pre>    public Foo() { }
    private Something _s;</pre>
<pre>    Something S</pre>
<pre>    {</pre>
<pre>        set { _s = value; }</pre>
<pre>        get { return _s; }</pre>
<pre>    }</pre>
<pre>}</pre>
Now, we see that Foo has a dependency on Something; it needs an instance of Something to be injected. We can use the Spring context to do this after we have created the instance of Foo:
<pre style="padding-left: 30px;">IApplicationContext context = ContextRegistry.GetContext();</pre>
<pre style="padding-left: 30px;">Foo f = new Foo();</pre>
<pre style="padding-left: 30px;">context.ConfigureObject(f, "fooPrototype");</pre>
But Spring does not yet know that the Foo instance needs to be injected Something. Hence, we need to tell Spring that by creating what I would call a &#8220;prototype&#8221; or &#8220;template&#8221; object configuration:
<pre style="padding-left: 30px;">&lt;object id="fooPrototype" type="ContextTestProject.Foo, ContextTestProject"&gt;</pre>
<pre style="padding-left: 30px;">   &lt;property name="S" ref="something"&gt;&lt;/property&gt;</pre>
<pre style="padding-left: 30px;">&lt;/object&gt;</pre>
The final step is then to register our newly created object in the Spring context:
<pre style="padding-left: 30px;">XmlApplicationContext xmlContext = context as XmlApplicationContext;</pre>
<pre style="padding-left: 30px;">xmlContext.ObjectFactory.RegisterSingleton("foo", f);</pre>
After this, the Foo instance is available for the application in the Spring context.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2008%2F08%2F07%2Fspringnet-programatically-add-objects-to-the-existing-xml-application-context%2F&amp;title=Spring.NET%3A%20programatically%20add%20objects%20to%20the%20existing%20%28XML%29%20application%20context" id="wpa2a_2"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2008/08/07/springnet-programatically-add-objects-to-the-existing-xml-application-context/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

