<?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>Roshan Kulkarni &#187; spring</title>
	<atom:link href="http://roshankulkarni.info/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://roshankulkarni.info</link>
	<description>On Technology, Web and Consulting</description>
	<lastBuildDate>Sun, 30 May 2010 18:28:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Referencing Spring Managed Beans from Non-Spring Java Code</title>
		<link>http://roshankulkarni.info/2010/05/referencing-spring-managed-beans-from-non-spring-java-code/</link>
		<comments>http://roshankulkarni.info/2010/05/referencing-spring-managed-beans-from-non-spring-java-code/#comments</comments>
		<pubDate>Tue, 04 May 2010 07:06:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://roshankulkarni.info/?p=115</guid>
		<description><![CDATA[I encountered the need to reference Spring managed beans from legacy Java code (i.e. Java code which is not managed by Spring). An ideal approach is to avoid mixed-mode code and Spring&#8217;ify everything. But that&#8217;s not always an option in reality. Legacy projects could have a incremental migration to Spring. Another option is to instantiate [...]]]></description>
			<content:encoded><![CDATA[<p>I encountered the need to reference Spring managed beans from legacy Java code (i.e. Java code which is not managed by Spring).</p>
<p>An ideal approach is to avoid mixed-mode code and Spring&#8217;ify everything. But that&#8217;s not always an option in reality. Legacy projects could have a incremental migration to Spring. </p>
<p>Another option is to instantiate the bean object in your non-managed code. Since you would craft such an object by hand, you have to carefully ensure that all the object dependencies are satisfied. If you miss initializing some properties, you are likely to end up with <em>NullPointerExceptions</em> at runtime. </p>
<p>A preferred approach is to let Spring create and initialize the object for you and to get a handle to that object in your legacy code. We create a <em><a href="http://static.springsource.org/spring/docs/1.1.4/api/org/springframework/context/ApplicationContextAware.html">ApplicationContextAware</a></em> bean as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.BeansException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.ApplicationContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.ApplicationContextAware</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpringApplicationContextAware <span style="color: #000000; font-weight: bold;">implements</span> ApplicationContextAware <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> ApplicationContext applicationContext<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setApplicationContext<span style="color: #009900;">&#40;</span>ApplicationContext context<span style="color: #009900;">&#41;</span>
                                                        <span style="color: #000000; font-weight: bold;">throws</span> BeansException <span style="color: #009900;">&#123;</span>
      applicationContext <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Object</span> getBean<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> beanName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> applicationContext.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span>beanName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We then declare a singleton instance of this bean using something like this in the Spring config:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;applicationContextAware&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.somedomain.somesubdomain.SpringApplicationContextAware&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>At the time of Spring initialization, Spring will instantiate the bean defined above. And since the class implements the <em>ApplicationContextAware</em> interface, Spring will invoke the setApplicationContext() method on it.</p>
<p>We can then access any named Spring bean as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">MyBean bean <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>MyBean<span style="color: #009900;">&#41;</span> SpringApplicationContext.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;whackyBeanName&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you prefer the new Annotation based configuration, then we can skip the <bean> declaration above and use something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:component-scan</span> <span style="color: #000066;">base-package</span>=<span style="color: #ff0000;">&quot;com.somedomain.somesubdomain&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>And use the @Component annotation on the bean as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.BeansException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.ApplicationContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.ApplicationContextAware</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Component</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpringApplicationContextAware <span style="color: #000000; font-weight: bold;">implements</span> ApplicationContextAware <span style="color: #009900;">&#123;</span>
	...
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://roshankulkarni.info/2010/05/referencing-spring-managed-beans-from-non-spring-java-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

