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

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

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

    ゞ沉默是金ゞ

    魚(yú)離不開(kāi)水,但是沒(méi)有說(shuō)不離開(kāi)哪滴水.
    posts - 98,comments - 104,trackbacks - 0
    在xml配置文件中,autowire有6種類型,可以在<bean/>元素中使用autowire屬性指定:
    模式                        說(shuō)明   
     no                       不使用自動(dòng)裝配,必須通過(guò)ref元素指定依賴,默認(rèn)設(shè)置。   
    byName                    根據(jù)屬性名自動(dòng)裝配。此選項(xiàng)將檢查容器并根據(jù)名字查找與                   
                              屬性完全一致的bean,并將其與屬性自動(dòng)裝配。   
    byType                    如果容器中存在一個(gè)與指定屬性類型相同的bean,那么將與   
                              該屬性自動(dòng)裝配;如果存在多個(gè)該類型bean,那么拋出異   
                              常,并指出不能使用byType方式進(jìn)行自動(dòng)裝配;如果沒(méi)有找   
                              到相匹配的bean,則什么事都不發(fā)生,也可以通過(guò)設(shè)置   
                              dependency
    -check="objects"讓Spring拋出異常。   
    constructor               與byType方式類似,不同之處在于它應(yīng)用于構(gòu)造器參數(shù)。如   
                              果容器中沒(méi)有找到與構(gòu)造器參數(shù)類型一致的bean,那么拋出   
                              異常。   
    autodetect                通過(guò)bean類的自省機(jī)制(introspection)來(lái)決定是使用   
                              constructor還是byType方式進(jìn)行自動(dòng)裝配。如果發(fā)現(xiàn)默認(rèn)的   
                              構(gòu)造器,那么將使用byType方式。  
    default
    看代碼:
    Computer.java
    package com.redoffice;

    public class Computer {
        
        
    private Host host;
        
    private Display display;
        
        
    public Computer(){
            
        }

        
        
    public Computer(Host host, Display display) {
            
    super();
            
    this.host = host;
            
    this.display = display;
        }


        
    public void run(){
            System.out.println();
            System.out.print(
    "   "+host.run()+",");
            System.out.println(display.run());
        }


        
    public Host getHost() {
            
    return host;
        }


        
    public void setHost(Host host) {
            
    this.host = host;
        }


        
    public Display getDisplay() {
            
    return display;
        }


        
    public void setDisplay(Display display) {
            
    this.display = display;
        }

    }


    Host.java
    package com.redoffice;

    public class Host {
        
    public String run() {
            
    return "我是主機(jī),正在運(yùn)行!";
        }

    }

    Display.java
    package com.redoffice;

    public class Display {
        
    public String run() {
            
    return "我是顯示器,正在運(yùn)行!";
        }

    }

    TestMain.java
    package com.redoffice;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.log4j.PropertyConfigurator;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class TestMain {
        
    private static Log log = LogFactory.getLog(TestMain.class);
        
        
    public static void main(String[] args) {
            PropertyConfigurator.configure(
    "bin/log4j.properties");
            log.debug(
    "entering main method");
            ApplicationContext ac 
    = new ClassPathXmlApplicationContext(
                    
    "applicationContext.xml");
            
    //byName
            Computer computer1 = (Computer)ac.getBean("computer1");
            System.out.println(
    "autowire=\"byName\":");
            computer1.run();
            
            
    //byType
            Computer computer2 = (Computer)ac.getBean("computer2");
            System.out.println(
    "autowire=\"byType\":");
            computer2.run();
            
            
    //default
            Computer computer3 = (Computer)ac.getBean("computer3");        
            System.out.println(
    "autowire=\"default\":");
            computer3.run();
            
            
    //autodetect
            Computer computer4 = (Computer)ac.getBean("computer4");        
            System.out.println(
    "autowire=\"autodetect\":");
            computer4.run();
            
            
    //constructor
            Computer computer5 = (Computer)ac.getBean("computer5");        
            System.out.println(
    "autowire=\"constructor\":");
            computer5.run();
            
            
    //no
            Computer computer6 = (Computer)ac.getBean("computer6");        
            System.out.println(
    "autowire=\"no\":");
            computer6.run();
            log.debug(
    "leaving main method");
        }

    }


    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
        
    default-autowire="autodetect">
        
    <bean id="host" name="host" class="com.redoffice.Host"></bean>
        
    <bean id="display" name="display" class="com.redoffice.Display"></bean>
        
        
    <!-- autowire有no,constructor,byName,byType,default,autodetect幾種狀況 -->
        
    <bean id="computer1" class="com.redoffice.Computer" autowire="byName"></bean>
        
    <bean id="computer2" class="com.redoffice.Computer" autowire="byType"></bean>
        
    <bean id="computer3" class="com.redoffice.Computer" autowire="default"></bean>
        
    <bean id="computer4" class="com.redoffice.Computer" autowire="autodetect"></bean>
        
    <!-- 在Computer類中必須有一個(gè)無(wú)參和有參的構(gòu)造函數(shù),否則報(bào)錯(cuò) -->
        
    <bean id="computer5" class="com.redoffice.Computer" autowire="constructor">
            
    <constructor-arg index="0">
                
    <ref bean="host"/>
            
    </constructor-arg>
            
    <constructor-arg index="1">
                
    <ref bean="display"/>
            
    </constructor-arg>
        
    </bean>
        
    <bean id="computer6" class="com.redoffice.Computer" autowire="no">
            
    <constructor-arg index="0">
                
    <ref bean="host"/>
            
    </constructor-arg>
            
    <constructor-arg index="1">
                
    <ref bean="display"/>
            
    </constructor-arg>
        
    </bean>
        
    </beans>

    希望有什么不對(duì)的地方多多指教。
    posted on 2009-04-03 10:28 ゞ沉默是金ゞ 閱讀(1711) 評(píng)論(4)  編輯  收藏 所屬分類: Spring基礎(chǔ)

    FeedBack:
    # re: Spring自動(dòng)裝配的學(xué)習(xí)
    2009-04-03 10:35 | asiawang
    自動(dòng)裝配 還是不用為好,會(huì)產(chǎn)生不可預(yù)知的錯(cuò)誤!  回復(fù)  更多評(píng)論
      
    # re: Spring自動(dòng)裝配的學(xué)習(xí)
    2009-04-03 11:41 | .........
    # re: Spring自動(dòng)裝配的學(xué)習(xí)
    2009-04-04 18:03 | Robin's Java World
    @asiawang
    我們?cè)陧?xiàng)目里用得蠻好的呀,不過(guò)最好只用byName方式。  回復(fù)  更多評(píng)論
      
    # re: Spring自動(dòng)裝配的學(xué)習(xí)[未登錄](méi)
    2010-08-22 13:10 | Sam
    寫的簡(jiǎn)潔易懂。
    其實(shí)第五點(diǎn),類沒(méi)有顯式寫出構(gòu)造函數(shù)的話,java不會(huì)再創(chuàng)造默認(rèn)構(gòu)造函數(shù)。所以你必須寫明無(wú)參構(gòu)造函數(shù)。

    很有用處  回復(fù)  更多評(píng)論
      

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 免费国产黄网站在线观看视频| 香蕉免费在线视频| 国产偷国产偷亚洲高清在线| 婷婷亚洲综合五月天小说在线| 成人午夜亚洲精品无码网站| 亚洲韩国精品无码一区二区三区| 亚洲AV中文无码乱人伦| 亚洲综合在线另类色区奇米| 亚洲电影国产一区| 亚洲欧洲日本在线观看| 特级av毛片免费观看| 你懂的免费在线观看网站| 亚洲一区二区免费视频| 国产免费观看黄AV片 | 最近免费中文字幕大全| 免费萌白酱国产一区二区| 亚洲乱码无码永久不卡在线 | WWW国产成人免费观看视频| 一级毛片aaaaaa免费看| 成年女人午夜毛片免费看| 亚洲精品亚洲人成在线观看下载| 亚洲国产精品va在线播放| 日韩亚洲产在线观看| 国产精品成人69XXX免费视频| 精品久久久久久久久免费影院| 亚洲av无码国产精品色在线看不卡| 久久精品亚洲中文字幕无码网站| 亚洲最大天堂无码精品区| 精品一区二区三区免费观看 | 91嫩草免费国产永久入口| 男女交性永久免费视频播放| 国产亚洲精品国产| 亚洲成a人片在线不卡一二三区| 在线亚洲高清揄拍自拍一品区| 四虎国产精品永免费| 每天更新的免费av片在线观看| 亚洲高清国产拍精品青青草原| 亚洲自偷精品视频自拍| 七次郎成人免费线路视频| 成年人免费的视频| 久久精品国产69国产精品亚洲|