<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Programmatically Build a Spring Application Context</title>
	<atom:link href="http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/</link>
	<description>Jing Xue's Weblog</description>
	<pubDate>Fri, 21 Nov 2008 12:31:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
		<item>
		<title>By: Jing Xue</title>
		<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-107841</link>
		<dc:creator>Jing Xue</dc:creator>
		<pubDate>Wed, 03 Sep 2008 12:46:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-107841</guid>
		<description>@Nick, you could try loading the "core" beans and the "plugin" beans into two different contexts (from two sets of XML files), and set the core context as the parent of the plugin one.</description>
		<content:encoded><![CDATA[<p>@Nick, you could try loading the &#8220;core&#8221; beans and the &#8220;plugin&#8221; beans into two different contexts (from two sets of XML files), and set the core context as the parent of the plugin one.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-107780</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Wed, 03 Sep 2008 01:28:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-107780</guid>
		<description>Re: hybrid approach 

I am trying to understand it as well. It looks like calling refresh() is mandatory on any context if new bean definitions were registered. As a result, the whole configuration is reloaded and singletons are recreated.

For plugin-like purposes I want to be able to append a new bean on-the-fly using beans from a static XML configuration as dependencies. I have not googled anything like that. Most links are about OSGI :(</description>
		<content:encoded><![CDATA[<p>Re: hybrid approach </p>
<p>I am trying to understand it as well. It looks like calling refresh() is mandatory on any context if new bean definitions were registered. As a result, the whole configuration is reloaded and singletons are recreated.</p>
<p>For plugin-like purposes I want to be able to append a new bean on-the-fly using beans from a static XML configuration as dependencies. I have not googled anything like that. Most links are about OSGI <img src='http://www.digizenstudio.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-102170</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Tue, 05 Aug 2008 18:56:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-102170</guid>
		<description>This is a great example. It is nice to see how to do this.

I do have one question though. Is there a way to do a hybrid approach where you define some of the configuration in xml and some of it programmatically as in your example?

I know it sounds like a strange request, but I do have some code where it would be very useful.</description>
		<content:encoded><![CDATA[<p>This is a great example. It is nice to see how to do this.</p>
<p>I do have one question though. Is there a way to do a hybrid approach where you define some of the configuration in xml and some of it programmatically as in your example?</p>
<p>I know it sounds like a strange request, but I do have some code where it would be very useful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Edison Gieswein</title>
		<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-48472</link>
		<dc:creator>Edison Gieswein</dc:creator>
		<pubDate>Fri, 05 Oct 2007 16:03:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-48472</guid>
		<description>I would just like to mention that I'm currently using this approach to load dynamically generated (from a database) groovy scripts.  I found the code listed above HIGHLY useful, although I ran across one small problem with the ScriptFactoryPostProcessor ...  you MUST ensure you call ctx.refresh(); after declaring your beans

I've included the code for the curious.

//XLogger.java
package com.xiotl.xdb.test;

public interface XLogger {
    void log(String msg);
}

//Test.java
package com.xiotl.xdb.test;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.scripting.support.ScriptFactoryPostProcessor;

public class Test {

    public static void main(String[] args) {
        
        //Declare the ApplicationContext
        GenericApplicationContext ctx = new GenericApplicationContext();

        //Define and register the post processor
        BeanDefinitionBuilder bdb1 = BeanDefinitionBuilder
                .rootBeanDefinition("org.springframework.scripting.support.ScriptFactoryPostProcessor")
                .setLazyInit(false);
        ctx.registerBeanDefinition("scriptPostProcessor", bdb1.getBeanDefinition());

        //define the script location...  in my case it is inline, 
        // but any standard classpath:/package/script.groovy style location can be used
        String location = ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + "package com.xiotl.xdb.test\n" +
                "\n" +
                "import org.apache.log4j.Logger\n" +
                "\n" +
                "class Scratch implements XLogger {\n" +
                "    private static Logger logger = Logger.getLogger(\"XLogger\");\n" +
                "\n" +
                "    public void log(String message) {\n" +
                "        logger.info(message);\n" +
                "    }\n" +
                "}";
        //define and register the groovy bean
        BeanDefinitionBuilder bdb2 = BeanDefinitionBuilder
                .rootBeanDefinition("org.springframework.scripting.groovy.GroovyScriptFactory")
                .addConstructorArg(location);
        ctx.registerBeanDefinition("groovyLogger", bdb2.getBeanDefinition());
        //refresh the context
        ctx.refresh();
        //get the bean
        XLogger xlog = (XLogger)ctx.getBean("groovyLogger");
        //test the bean
        xlog.log("A Message from Groovy");
    }

    
}</description>
		<content:encoded><![CDATA[<p>I would just like to mention that I&#8217;m currently using this approach to load dynamically generated (from a database) groovy scripts.  I found the code listed above HIGHLY useful, although I ran across one small problem with the ScriptFactoryPostProcessor &#8230;  you MUST ensure you call ctx.refresh(); after declaring your beans</p>
<p>I&#8217;ve included the code for the curious.</p>
<p>//XLogger.java<br />
package com.xiotl.xdb.test;</p>
<p>public interface XLogger {<br />
    void log(String msg);<br />
}</p>
<p>//Test.java<br />
package com.xiotl.xdb.test;</p>
<p>import org.springframework.beans.factory.support.BeanDefinitionBuilder;<br />
import org.springframework.context.support.GenericApplicationContext;<br />
import org.springframework.scripting.support.ScriptFactoryPostProcessor;</p>
<p>public class Test {</p>
<p>    public static void main(String[] args) {</p>
<p>        //Declare the ApplicationContext<br />
        GenericApplicationContext ctx = new GenericApplicationContext();</p>
<p>        //Define and register the post processor<br />
        BeanDefinitionBuilder bdb1 = BeanDefinitionBuilder<br />
                .rootBeanDefinition(&#8221;org.springframework.scripting.support.ScriptFactoryPostProcessor&#8221;)<br />
                .setLazyInit(false);<br />
        ctx.registerBeanDefinition(&#8221;scriptPostProcessor&#8221;, bdb1.getBeanDefinition());</p>
<p>        //define the script location&#8230;  in my case it is inline,<br />
        // but any standard classpath:/package/script.groovy style location can be used<br />
        String location = ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + &#8220;package com.xiotl.xdb.test\n&#8221; +<br />
                &#8220;\n&#8221; +<br />
                &#8220;import org.apache.log4j.Logger\n&#8221; +<br />
                &#8220;\n&#8221; +<br />
                &#8220;class Scratch implements XLogger {\n&#8221; +<br />
                &#8221;    private static Logger logger = Logger.getLogger(\&#8221;XLogger\&#8221;);\n&#8221; +<br />
                &#8220;\n&#8221; +<br />
                &#8221;    public void log(String message) {\n&#8221; +<br />
                &#8221;        logger.info(message);\n&#8221; +<br />
                &#8221;    }\n&#8221; +<br />
                &#8220;}&#8221;;<br />
        //define and register the groovy bean<br />
        BeanDefinitionBuilder bdb2 = BeanDefinitionBuilder<br />
                .rootBeanDefinition(&#8221;org.springframework.scripting.groovy.GroovyScriptFactory&#8221;)<br />
                .addConstructorArg(location);<br />
        ctx.registerBeanDefinition(&#8221;groovyLogger&#8221;, bdb2.getBeanDefinition());<br />
        //refresh the context<br />
        ctx.refresh();<br />
        //get the bean<br />
        XLogger xlog = (XLogger)ctx.getBean(&#8221;groovyLogger&#8221;);<br />
        //test the bean<br />
        xlog.log(&#8221;A Message from Groovy&#8221;);<br />
    }</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jing Xue</title>
		<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-42224</link>
		<dc:creator>Jing Xue</dc:creator>
		<pubDate>Tue, 17 Jul 2007 17:00:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-42224</guid>
		<description>@Nirav,
Are all thousands of test cases actually going to run concurrently? Usually test cases are executed in sequence and whatever objects one test case holds are gc'ed after the case is torn down.</description>
		<content:encoded><![CDATA[<p>@Nirav,<br />
Are all thousands of test cases actually going to run concurrently? Usually test cases are executed in sequence and whatever objects one test case holds are gc&#8217;ed after the case is torn down.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nirav Gosalia</title>
		<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-42156</link>
		<dc:creator>Nirav Gosalia</dc:creator>
		<pubDate>Mon, 16 Jul 2007 08:11:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-42156</guid>
		<description>Hi,

i am using the programmatic approach for bean creation to create a test harness which has its own XML language whose xsd is registered with Spring. Every thing is working perfectly but i was wondering what will happen when thousands of test cases will be run together. Each test case will create its own object and there will be a large number of objects in memory. Will this cause a problem ? Any Thoughts ? work arounds ?</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>i am using the programmatic approach for bean creation to create a test harness which has its own XML language whose xsd is registered with Spring. Every thing is working perfectly but i was wondering what will happen when thousands of test cases will be run together. Each test case will create its own object and there will be a large number of objects in memory. Will this cause a problem ? Any Thoughts ? work arounds ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Bosch</title>
		<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-25193</link>
		<dc:creator>Mike Bosch</dc:creator>
		<pubDate>Mon, 15 Jan 2007 18:02:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-25193</guid>
		<description>While for simple cases the XML configuration is simpler you might find that in cases where you have an enterprise application where it gets customized on a per deployment basis that the programmatic approach offers a lot more flexibility and capabilities than you could do with the XML option.  I was working on some issues at my previous job about trying to build a custom ApplicationContext and this sort of thing comes in very handy vs. trying to figure out which XML files to load and so forth.  Each option has its uses.  

If you're building a hosted app or single deployment type apps (i.e. desktop) there are probably very few areas I would use this for unless I was loading a lot of smaller ApplicationContexts or child contexts where they were dynamic based on say, user or account settings.  

However, coding is like the English language.  There's always an exception to every rule.</description>
		<content:encoded><![CDATA[<p>While for simple cases the XML configuration is simpler you might find that in cases where you have an enterprise application where it gets customized on a per deployment basis that the programmatic approach offers a lot more flexibility and capabilities than you could do with the XML option.  I was working on some issues at my previous job about trying to build a custom ApplicationContext and this sort of thing comes in very handy vs. trying to figure out which XML files to load and so forth.  Each option has its uses.  </p>
<p>If you&#8217;re building a hosted app or single deployment type apps (i.e. desktop) there are probably very few areas I would use this for unless I was loading a lot of smaller ApplicationContexts or child contexts where they were dynamic based on say, user or account settings.  </p>
<p>However, coding is like the English language.  There&#8217;s always an exception to every rule.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon Cition</title>
		<link>http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-25134</link>
		<dc:creator>Simon Cition</dc:creator>
		<pubDate>Mon, 15 Jan 2007 00:56:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.digizenstudio.com/blog/2007/01/14/programmatically-build-a-spring-application-context/#comment-25134</guid>
		<description>I would have thought the XML configuration would be easier to set-up than programming it in Java or even some other script. 
I think that a programmatic configuration is warranted where a level of dynamic configuration may be required, for example, driven from a database. This could be in combination with a base XML configuration.</description>
		<content:encoded><![CDATA[<p>I would have thought the XML configuration would be easier to set-up than programming it in Java or even some other script.<br />
I think that a programmatic configuration is warranted where a level of dynamic configuration may be required, for example, driven from a database. This could be in combination with a base XML configuration.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
