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

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

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

    ゞ沉默是金ゞ

    魚離不開水,但是沒有說不離開哪滴水.
    posts - 98,comments - 104,trackbacks - 0
    在xml配置文件中,autowire有6種類型,可以在<bean/>元素中使用autowire屬性指定:
    模式                        說明   
     no                       不使用自動裝配,必須通過ref元素指定依賴,默認設置。   
    byName                    根據屬性名自動裝配。此選項將檢查容器并根據名字查找與                   
                              屬性完全一致的bean,并將其與屬性自動裝配。   
    byType                    如果容器中存在一個與指定屬性類型相同的bean,那么將與   
                              該屬性自動裝配;如果存在多個該類型bean,那么拋出異   
                              常,并指出不能使用byType方式進行自動裝配;如果沒有找   
                              到相匹配的bean,則什么事都不發生,也可以通過設置   
                              dependency
    -check="objects"讓Spring拋出異常。   
    constructor               與byType方式類似,不同之處在于它應用于構造器參數。如   
                              果容器中沒有找到與構造器參數類型一致的bean,那么拋出   
                              異常。   
    autodetect                通過bean類的自省機制(introspection)來決定是使用   
                              constructor還是byType方式進行自動裝配。如果發現默認的   
                              構造器,那么將使用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 "我是主機,正在運行!";
        }

    }

    Display.java
    package com.redoffice;

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

    }

    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類中必須有一個無參和有參的構造函數,否則報錯 -->
        
    <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>

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

    FeedBack:
    # re: Spring自動裝配的學習
    2009-04-03 10:35 | asiawang
    自動裝配 還是不用為好,會產生不可預知的錯誤!  回復  更多評論
      
    # re: Spring自動裝配的學習
    2009-04-03 11:41 | .........
    是啊.  回復  更多評論
      
    # re: Spring自動裝配的學習
    2009-04-04 18:03 | Robin's Java World
    @asiawang
    我們在項目里用得蠻好的呀,不過最好只用byName方式。  回復  更多評論
      
    # re: Spring自動裝配的學習[未登錄]
    2010-08-22 13:10 | Sam
    寫的簡潔易懂。
    其實第五點,類沒有顯式寫出構造函數的話,java不會再創造默認構造函數。所以你必須寫明無參構造函數。

    很有用處  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国外成人免费高清激情视频| 亚洲AV网站在线观看| av无码免费一区二区三区| 国产亚洲综合色就色| 丁香花在线视频观看免费 | 久久受www免费人成_看片中文| 狠狠综合久久综合88亚洲| 无码人妻一区二区三区免费视频| 精品免费国产一区二区| 亚洲熟女精品中文字幕| 成人免费a级毛片无码网站入口| 亚洲国产精品白丝在线观看| 精品国产免费人成电影在线观看 | 亚洲国产成人久久一区二区三区| 免费看又爽又黄禁片视频1000| 九一在线完整视频免费观看| 亚洲一区无码中文字幕| 日本亚洲欧洲免费天堂午夜看片女人员| 国产亚洲av片在线观看播放 | 成人免费视频77777| 亚洲日韩精品国产一区二区三区| 成人毛片18女人毛片免费| 中国极品美軳免费观看| 亚洲视频在线一区| 免费黄色福利视频| 国产精品1024在线永久免费| 亚洲AV午夜成人片| 波多野结衣在线免费观看| 二个人看的www免费视频| 亚洲精品天堂无码中文字幕| 亚洲日韩中文字幕| 亚洲av永久无码精品网站| 亚洲国产成人久久笫一页| 浮力影院第一页小视频国产在线观看免费| 日韩免费电影网站| 成人av片无码免费天天看| 色多多免费视频观看区一区| 亚洲av午夜福利精品一区人妖| 亚洲AV无码不卡在线观看下载| 大陆一级毛片免费视频观看i| 亚洲免费视频播放|