<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Dict.CN 在線詞典, 英語學(xué)習(xí), 在線翻譯

    都市淘沙者

    荔枝FM Everyone can be host

    統(tǒng)計

    留言簿(23)

    積分與排名

    優(yōu)秀學(xué)習(xí)網(wǎng)站

    友情連接

    閱讀排行榜

    評論排行榜

    Spring中IOC的實(shí)現(xiàn) (轉(zhuǎn))


                  了解了IOC模式的思想以及其優(yōu)點(diǎn),再來學(xué)習(xí)其實(shí)現(xiàn)。上篇blog中大致描述了PicoContainer以及Spring各自對IOC的實(shí)現(xiàn),這篇來詳細(xì)看一下Spring中它的實(shí)現(xiàn)。

    SpringIOC貫穿了其整個框架,但正如martinflower所說:“saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels”,IOC已經(jīng)稱為框架設(shè)計中必不可少的部分。就實(shí)現(xiàn)上來講Spring采取了配置文件的形式來實(shí)現(xiàn)依賴的注射,并且支持Type2 IOCSetter Injection)以及Type3 IOCConstructor Injection)。

         SpringIOC的實(shí)現(xiàn)的核心是其Core Bean Factory,它將框架內(nèi)部的組件以一定的耦合度組裝起來,并對使用它的應(yīng)用提供一種面向服務(wù)的編程模式(SOPService-Orient Programming),比如Spring中的AOP、以及持久化(Hibernateibatics)的實(shí)現(xiàn)。
        首先從最底層最基礎(chǔ)的factory Bean開始,先來看org.springframework.beans.factory.Bean

    Factory接口,它是一個非常簡單的接口,getBean方法是其中最重要的方法,Spring通常是使用xmlpopulate Bean,所以比較常用的是XMLFactoryBean


    用一個簡單的示例看一下其用法。首先寫下兩個
    Bean類:

    ExampleBean 類:
    public class ExampleBean {

           private String psnName=null;

           private RefBean refbean=null;

           private String addinfo=null;

           public String getAddinfo() {

                  return getRefbean().getAddress()+getRefbean().getZipcode();

           }

           public String getPsnName() {

                  return psnName;

           }

           public void setPsnName(String psnName) {

                  this.psnName = psnName;

           }

           public void setRefbean(RefBean refbean) {

                  this.refbean = refbean;

           }

           public RefBean getRefbean() {

                  return refbean;

           }

           public void setAddinfo(String addinfo) {
                  this.addinfo = addinfo;
           }
    }

    RefBean
    類:

    public class RefBean {

           public String getAddress() {

                  return address;

           }

           public void setAddress(String address) {

                  this.address = address;

           }

           public String getZipcode() {

                  return zipcode;

           }

           public void setZipcode(String zipcode) {

                  this.zipcode = zipcode;

           }

           private String zipcode=null;

           private String address=null;

    }


    xml配置文件 Bean.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

      <bean id="exampleBean" class="test.ExampleBean">

        <property name="psnName"><value>xkf</value></property>

        <property name="refbean">

           <ref bean="refBean"/>

        </property>

      </bean>

      <bean id="refBean" class="test.RefBean">

      <property name="address"><value>BeiJing</value></property>

      <property name="zipcode"><value>100085</value></property>

      </bean>

    </beans>

     

        然后可以寫個測試類來測試,當(dāng)然,需要Spring中的Spring-core.jar以及commons-logging.jar,當(dāng)然在elipse中可以通過安裝spring-ide插件來輕松實(shí)現(xiàn)。

    public class Test {

           public static void main(String[] args){

                  try{

                  Resource input = new ClassPathResource("test/Bean.xml");

                  System.out.println("resource is:"+input);

                  BeanFactory factory = new XmlBeanFactory(input);

                  ExampleBean eb =

                  (ExampleBean)factory.getBean("exampleBean");

                  System.out.println(eb.getPsnName());

                  System.out.println(eb.getAddinfo());

           }

           catch(Exception e){

                  e.printStackTrace();

           }

    }


         這樣,通過
    BeanFactorygetBean方法,以及xml配置文件,避免了在test類中直接實(shí)例化ExampleBean,消除了應(yīng)用程序(Test)與服務(wù)(ExampleBean)之間的耦合,實(shí)現(xiàn)了IOC(控制反轉(zhuǎn))或者說實(shí)現(xiàn)了依賴的注射(Dependency Injection)。

    posted on 2006-02-24 20:53 都市淘沙者 閱讀(1807) 評論(0)  編輯  收藏 所屬分類: Spring+Struts+Hibernate

    主站蜘蛛池模板: 日韩免费高清一级毛片| 黄色a级片免费看| a高清免费毛片久久| 91精品手机国产免费| 亚洲国产综合人成综合网站| ass亚洲**毛茸茸pics| 在线人成免费视频69国产| 国产免费卡一卡三卡乱码| 亚洲人成人网毛片在线播放| 99爱免费观看视频在线| 亚洲国产精品VA在线看黑人| 美景之屋4在线未删减免费| 亚洲AV中文无码乱人伦下载| 亚洲精品黄色视频在线观看免费资源| 亚洲成人激情在线| 99精品免费观看| 深夜特黄a级毛片免费播放| 五月天网站亚洲小说| 国产免费资源高清小视频在线观看| 日韩电影免费在线观看| 亚洲bt加勒比一区二区| 日本视频免费在线| 亚洲成a人无码亚洲成av无码| 成年18网站免费视频网站| 亚洲 欧洲 日韩 综合在线| 亚洲精品乱码久久久久久中文字幕 | 岛国片在线免费观看| 亚洲狠狠婷婷综合久久蜜芽| 国产亚洲精品国产| 国产在线观看www鲁啊鲁免费| 日本激情猛烈在线看免费观看 | 免费a级毛片无码av| 三根一起会坏掉的好痛免费三级全黄的视频在线观看 | 免费观看毛片视频| 国产成人综合久久精品亚洲| 四虎AV永久在线精品免费观看| 亚洲精品视频在线免费| 中文日韩亚洲欧美制服| 亚洲欧洲视频在线观看| 国产乱码免费卡1卡二卡3卡| 亚洲熟妇AV一区二区三区浪潮|