Spring的@Configuration來代替xml配置

<div class="htmledit_views">html

<p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"><strong>一. Xml配置法</strong></p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">下面是一個典型的<a href="http://lib.csdn.net/base/javaee" class="replace_word" title="Java EE知識庫" style="color:rgb(223,52,52);font-weight:bold;" target="_blank">spring</a>配置文件(<span style="font-family:courier;">application-config.xml</span>):</p> <div class="dp-highlighter bg_xml" style="font-family:Consolas, 'Courier New', Courier, mono, serif;background-color:rgb(231,229,220);padding-top:1px;color:rgb(51,51,51);margin:18px 0px !important;"> <div class="bar" style="padding-left:45px;"> <div class="tools" style="padding:3px 8px 10px 10px;font-size:9px;line-height:normal;font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;color:#C0C0C0;background-color:rgb(248,248,248);border-left:3px solid rgb(108,226,108);"> <strong>[xml]</strong>&nbsp;<a href="http://blog.csdn.net/tanksyg/article/details/8556769#" class="ViewSource" title="view plain" style="color:rgb(160,160,160);background-color:inherit;border:none;padding:1px;margin:0px 10px 0px 0px;font-size:9px;display:inline-block;text-indent:-2000px;" target="_blank">view plain</a><span>&nbsp;<a href="http://blog.csdn.net/tanksyg/article/details/8556769#" class="CopyToClipboard" title="copy" style="color:rgb(160,160,160);background-color:inherit;border:none;padding:1px;margin:0px 10px 0px 0px;font-size:9px;display:inline-block;text-indent:-2000px;" target="_blank">copy</a><div style="position: absolute; left: 221px; top: 411px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 221px; top: 411px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_5" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&amp;width=16&amp;height=16" wmode="transparent"></div></span> <div style="z-index:99;"> </div> <span></span></div> </div> <ol start="1" class="dp-xml" style="padding:0px;border:none;background-color:rgb(255,255,255);color:rgb(92,92,92);margin:0px 0px 1px 45px !important;"><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;"><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&lt;</span><span class="tag-name" style="margin:0px;padding:0px;border:none;color:rgb(153,51,0);background-color:inherit;font-weight:bold;">beans</span><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&gt;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&lt;</span><span class="tag-name" style="margin:0px;padding:0px;border:none;color:rgb(153,51,0);background-color:inherit;font-weight:bold;">bean</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="attribute" style="margin:0px;padding:0px;border:none;color:#FF0000;background-color:inherit;">id</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">=</span><span class="attribute-value" style="margin:0px;padding:0px;border:none;color:#0000FF;background-color:inherit;">"orderService"</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="attribute" style="margin:0px;padding:0px;border:none;color:#FF0000;background-color:inherit;">class</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">=</span><span class="attribute-value" style="margin:0px;padding:0px;border:none;color:#0000FF;background-color:inherit;">"com.acme.OrderService"</span><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">/&gt;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&lt;</span><span class="tag-name" style="margin:0px;padding:0px;border:none;color:rgb(153,51,0);background-color:inherit;font-weight:bold;">constructor-arg</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="attribute" style="margin:0px;padding:0px;border:none;color:#FF0000;background-color:inherit;">ref</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">=</span><span class="attribute-value" style="margin:0px;padding:0px;border:none;color:#0000FF;background-color:inherit;">"orderRepository"</span><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">/&gt;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&lt;/</span><span class="tag-name" style="margin:0px;padding:0px;border:none;color:rgb(153,51,0);background-color:inherit;font-weight:bold;">bean</span><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&gt;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&lt;</span><span class="tag-name" style="margin:0px;padding:0px;border:none;color:rgb(153,51,0);background-color:inherit;font-weight:bold;">bean</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="attribute" style="margin:0px;padding:0px;border:none;color:#FF0000;background-color:inherit;">id</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">=</span><span class="attribute-value" style="margin:0px;padding:0px;border:none;color:#0000FF;background-color:inherit;">"orderRepository"</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="attribute" style="margin:0px;padding:0px;border:none;color:#FF0000;background-color:inherit;">class</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">=</span><span class="attribute-value" style="margin:0px;padding:0px;border:none;color:#0000FF;background-color:inherit;">"com.acme.OrderRepository"</span><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">/&gt;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&lt;</span><span class="tag-name" style="margin:0px;padding:0px;border:none;color:rgb(153,51,0);background-color:inherit;font-weight:bold;">constructor-arg</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="attribute" style="margin:0px;padding:0px;border:none;color:#FF0000;background-color:inherit;">ref</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">=</span><span class="attribute-value" style="margin:0px;padding:0px;border:none;color:#0000FF;background-color:inherit;">"dataSource"</span><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">/&gt;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&lt;/</span><span class="tag-name" style="margin:0px;padding:0px;border:none;color:rgb(153,51,0);background-color:inherit;font-weight:bold;">bean</span><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&gt;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;"><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&lt;/</span><span class="tag-name" style="margin:0px;padding:0px;border:none;color:rgb(153,51,0);background-color:inherit;font-weight:bold;">beans</span><span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;">&gt;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li></ol></div> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">而後你就能夠像這樣來使用是bean了:</p> <div class="dp-highlighter bg_java" style="font-family:Consolas, 'Courier New', Courier, mono, serif;background-color:rgb(231,229,220);padding-top:1px;color:rgb(51,51,51);margin:18px 0px !important;"> <div class="bar" style="padding-left:45px;"> <div class="tools" style="padding:3px 8px 10px 10px;font-size:9px;line-height:normal;font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;color:#C0C0C0;background-color:rgb(248,248,248);border-left:3px solid rgb(108,226,108);"> <strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/tanksyg/article/details/8556769#" class="ViewSource" title="view plain" style="color:rgb(160,160,160);background-color:inherit;border:none;padding:1px;margin:0px 10px 0px 0px;font-size:9px;display:inline-block;text-indent:-2000px;" target="_blank">view plain</a><span>&nbsp;<a href="http://blog.csdn.net/tanksyg/article/details/8556769#" class="CopyToClipboard" title="copy" style="color:rgb(160,160,160);background-color:inherit;border:none;padding:1px;margin:0px 10px 0px 0px;font-size:9px;display:inline-block;text-indent:-2000px;" target="_blank">copy</a><div style="position: absolute; left: 225px; top: 648px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 225px; top: 648px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_6" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&amp;width=16&amp;height=16" wmode="transparent"></div></span> <div style="z-index:99;"> </div> <span></span></div> </div> <ol start="1" class="dp-j" style="padding:0px;border:none;background-color:rgb(255,255,255);color:rgb(92,92,92);margin:0px 0px 1px 45px !important;"><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;"><span style="margin:0px;padding:0px;border:none;background-color:inherit;">ApplicationContext&nbsp;ctx&nbsp;=&nbsp;</span><span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">new</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;ClassPathXmlApplicationContext(</span><span class="string" style="margin:0px;padding:0px;border:none;color:#0000FF;background-color:inherit;">"application-config.xml"</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">);&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">OrderService&nbsp;orderService&nbsp;=&nbsp;(OrderService)&nbsp;ctx.getBean(<span class="string" style="margin:0px;padding:0px;border:none;color:#0000FF;background-color:inherit;">"orderService"</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">);&nbsp;&nbsp;</span></span></li></ol></div> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"><br></p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"><br></p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"><strong>二. @Configuration配置法</strong></p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">如今Spring&nbsp;<a href="http://lib.csdn.net/base/java" class="replace_word" title="Java 知識庫" style="color:rgb(223,52,52);font-weight:bold;" target="_blank">Java&nbsp;</a>Configuration這個項目提供了一種經過java代碼來裝配bean的方案:</p> <div class="dp-highlighter" style="font-family:Consolas, 'Courier New', Courier, mono, serif;background-color:rgb(231,229,220);padding-top:1px;color:rgb(51,51,51);margin:18px 0px !important;"> <div class="dp-highlighter bg_java" style="padding-top:1px;margin:18px 0px !important;"> <div class="bar" style="padding-left:45px;"> <div class="tools" style="padding:3px 8px 10px 10px;font-size:9px;line-height:normal;font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;color:#C0C0C0;background-color:rgb(248,248,248);border-left:3px solid rgb(108,226,108);"> <strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/tanksyg/article/details/8556769#" class="ViewSource" title="view plain" style="color:rgb(160,160,160);background-color:inherit;border:none;padding:1px;margin:0px 10px 0px 0px;font-size:9px;display:inline-block;text-indent:-2000px;" target="_blank">view plain</a><span>&nbsp;<a href="http://blog.csdn.net/tanksyg/article/details/8556769#" class="CopyToClipboard" title="copy" style="color:rgb(160,160,160);background-color:inherit;border:none;padding:1px;margin:0px 10px 0px 0px;font-size:9px;display:inline-block;text-indent:-2000px;" target="_blank">copy</a><div style="position: absolute; left: 225px; top: 922px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 225px; top: 922px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_7" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&amp;width=16&amp;height=16" wmode="transparent"></div></span> <div style="z-index:99;"> </div> <span></span></div> </div> <ol start="1" class="dp-j" style="padding:0px;border:none;background-color:rgb(255,255,255);color:rgb(92,92,92);margin:0px 0px 1px 45px !important;"><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;"><span class="annotation" style="margin:0px;padding:0px;border:none;color:rgb(100,100,100);background-color:inherit;">@Configuration</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;"><span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">public</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">class</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;ApplicationConfig&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="white-space:pre;"></span><span style="white-space:pre;"><span style="color:rgb(100,100,100);font-family:Consolas, 'Courier New', Courier, mono, serif;background-color:rgb(248,248,248);">@Bean</span></span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">public</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;OrderService&nbsp;orderService()&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">return</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">new</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;OrderService(orderRepository());&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="white-space:pre;"></span><span style="white-space:pre;"><span style="color:rgb(100,100,100);font-family:Consolas, 'Courier New', Courier, mono, serif;background-color:rgb(248,248,248);">@Bean</span></span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">public</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">OrderRepository&nbsp;orderRepository()&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">return</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;</span><span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">new</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;OrderRepository(dataSource());&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;<span style="white-space:pre;"></span>&nbsp; &nbsp; &nbsp; &nbsp;<span style="white-space:pre;"><span style="color:rgb(100,100,100);font-family:Consolas, 'Courier New', Courier, mono, serif;background-color:rgb(248,248,248);">@Bean</span></span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">public</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;DataSource&nbsp;dataSource()&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="margin:0px;padding:0px;border:none;color:rgb(0,130,0);background-color:inherit;">//&nbsp;instantiate&nbsp;and&nbsp;return&nbsp;an&nbsp;new&nbsp;DataSource&nbsp;…</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">}&nbsp;&nbsp;</span></li></ol></div> <div class="save_code tracking-ad" style="font-size:14px;color:rgb(255,255,255);z-index:2;"> <a style="color:rgb(51,102,153);background:none;border:none;padding:0px;margin:0px;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png" alt="" style="border:none;"></a></div> <div class="save_code tracking-ad" style="font-size:14px;color:rgb(255,255,255);z-index:2;"> <a style="color:rgb(51,102,153);background:none;border:none;padding:0px;margin:0px;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png" alt="" style="border:none;"></a></div> </div> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">而後你就能夠像這樣來使用是bean了:</p> <div class="dp-highlighter bg_java" style="font-family:Consolas, 'Courier New', Courier, mono, serif;background-color:rgb(231,229,220);padding-top:1px;color:rgb(51,51,51);margin:18px 0px !important;"> <div class="bar" style="padding-left:45px;"> <div class="tools" style="padding:3px 8px 10px 10px;font-size:9px;line-height:normal;font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;color:#C0C0C0;background-color:rgb(248,248,248);border-left:3px solid rgb(108,226,108);"> <strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/tanksyg/article/details/8556769#" class="ViewSource" title="view plain" style="color:rgb(160,160,160);background-color:inherit;border:none;padding:1px;margin:0px 10px 0px 0px;font-size:9px;display:inline-block;text-indent:-2000px;" target="_blank">view plain</a><span>&nbsp;<a href="http://blog.csdn.net/tanksyg/article/details/8556769#" class="CopyToClipboard" title="copy" style="color:rgb(160,160,160);background-color:inherit;border:none;padding:1px;margin:0px 10px 0px 0px;font-size:9px;display:inline-block;text-indent:-2000px;" target="_blank">copy</a><div style="position: absolute; left: 225px; top: 1303px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_4" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&amp;width=16&amp;height=16" wmode="transparent"></div><div style="position: absolute; left: 225px; top: 1303px; width: 16px; height: 16px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&amp;width=16&amp;height=16" wmode="transparent"></div></span> <div style="z-index:99;"> </div> <span></span></div> </div> <ol start="1" class="dp-j" style="padding:0px;border:none;background-color:rgb(255,255,255);color:rgb(92,92,92);margin:0px 0px 1px 45px !important;"><li class="alt" style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);color:inherit;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;"><span style="margin:0px;padding:0px;border:none;background-color:inherit;">JavaConfigApplicationContext&nbsp;ctx&nbsp;=&nbsp;</span><span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">new</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">&nbsp;JavaConfigApplicationContext(ApplicationConfig.</span><span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">class</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">);&nbsp;&nbsp;</span></span></li><li style="border-top:none;border-right:none;border-bottom:none;border-left:3px solid rgb(108,226,108);background-color:rgb(248,248,248);line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;list-style-position:outside !important;"> <span style="margin:0px;padding:0px;border:none;color:#000000;background-color:inherit;">OrderService&nbsp;orderService&nbsp;=&nbsp;ctx.getBean(OrderService.<span class="keyword" style="margin:0px;padding:0px;border:none;color:rgb(0,102,153);background-color:inherit;font-weight:bold;">class</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">);&nbsp;&nbsp;</span></span></li></ol></div> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"><strong>&nbsp;</strong></p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"><strong>三. 這麼作有什麼好處呢?</strong></p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp; 1.使用純java代碼,不在須要xml</p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2.在配置中也可享受OO帶來的好處</p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp; 3.類型安全對重構也能提供良好的支持</p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp; 4.依舊能享受到全部springIoC容器提供的功能</p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"><br></p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;">轉載自:http://blog.csdn.net/tanksyg/article/details/8556769</p> </div>java

相關文章
相關標籤/搜索