<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> <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> <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&width=16&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&width=16&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;"><</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;">></span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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;"><</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;"> </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;"> </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;">/></span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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;"> <span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;"><</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;"> </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;">/></span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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;"></</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;">></span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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;"> <span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;"><</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;"> </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;"> </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;">/></span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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;"><</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;"> </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;">/></span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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;"> <span class="tag" style="color:rgb(153,51,0);margin:0px;padding:0px;border:none;background-color:inherit;font-weight:bold;"></</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;">></span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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;"></</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;">></span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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> <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> <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&width=16&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&width=16&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 ctx = </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;"> 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;">); </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 orderService = (OrderService) 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;">); </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 <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 </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> <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> <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&width=16&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&width=16&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;"> </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;"> </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;"> ApplicationConfig { </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;"> <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;"> <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;"> OrderService orderService() { </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;"> <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;"> </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;"> OrderService(orderRepository()); </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></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;"> <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;"> <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;"> </span><span style="margin:0px;padding:0px;border:none;background-color:inherit;">OrderRepository orderRepository() { </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;"> <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;"> </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;"> OrderRepository(dataSource()); </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></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;"> <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;"> <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;"> DataSource dataSource() { </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;"> <span class="comment" style="margin:0px;padding:0px;border:none;color:rgb(0,130,0);background-color:inherit;">// instantiate and return an new DataSource …</span><span style="margin:0px;padding:0px;border:none;background-color:inherit;"> </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></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;">} </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> <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> <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&width=16&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&width=16&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 ctx = </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;"> 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;">); </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 orderService = 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;">); </span></span></li></ol></div> <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;"><strong>三. 這麼作有什麼好處呢?</strong></p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"> 1.使用純java代碼,不在須要xml</p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"> 2.在配置中也可享受OO帶來的好處</p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"> 3.類型安全對重構也能提供良好的支持</p> <p style="color:rgb(51,51,51);font-family:Arial;font-size:14px;"> 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