熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring自動裝配的學習

2022-06-13   來源: Java開源技術 

  在xml配置文件中autowire有種類型可以在<bean/>元素中使用autowire屬性指定

   模式                        說明   
 no                       不使用自動裝配必須通過ref元素指定依賴默認設置   
byName                    根據屬性名自動裝配此選項將檢查容器並根據名字查找與                   
                          屬性完全一致的bean並將其與屬性自動裝配   
byType                    如果容器中存在一個與指定屬性類型相同的bean那麼將與   
                          該屬性自動裝配如果存在多個該類型bean那麼拋出異   
                          常並指出不能使用byType方式進行自動裝配如果沒有找   
                          到相匹配的bean則什麼事都不發生也可以通過設置   
                          dependencycheck=objects讓Spring拋出異常   
constructor               與byType方式類似不同之處在於它應用於構造器參數如   
                          果容器中沒有找到與構造器參數類型一致的bean那麼拋出   
                          異常   
autodetect                通過bean類的自省機制(introspection)來決定是使用   
                          constructor還是byType方式進行自動裝配如果發現默認的   
                          構造器那麼將使用byType方式  
default

  看代碼Computerjava

   package comredoffice;

public class Computer {
    
    private Host host;
    private Display display;
    
    public Computer(){
        
    }
    
    public Computer(Host host Display display) {
        super();
        thishost = host;
        thisdisplay = display;
    }

    public void run(){
        Systemoutprintln();
        Systemoutprint(   +hostrun()+);
        Systemoutprintln(displayrun());
    }

    public Host getHost() {
        return host;
    }

    public void setHost(Host host) {
        thishost = host;
    }

    public Display getDisplay() {
        return display;
    }

    public void setDisplay(Display display) {
        thisdisplay = display;
    }
}

  Hostjava

   package comredoffice;

public class Host {
    public String run() {
        return 我是主機正在運行!;
    }
}

  Displayjava

   package comredoffice;

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

  TestMainjava

   package comredoffice;

import monsloggingLog;
import monsloggingLogFactory;
import orgapachelogjPropertyConfigurator;
import orgntextApplicationContext;
import orgntextsupportClassPathXmlApplicationContext;

public class TestMain {
    private static Log log = LogFactorygetLog(TestMainclass);
    
    public static void main(String[] args) {
        Propnfigure(bin/logjproperties);
        logdebug(entering main method);
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                applicationContextxml);
        //byName
        Computer computer = (Computer)acgetBean(computer);
        Systemoutprintln(autowire=\byName\:);
        computerrun();
        
        //byType
        Computer computer = (Computer)acgetBean(computer);
        Systemoutprintln(autowire=\byType\:);
        computerrun();
        
        //default
        Computer computer = (Computer)acgetBean(computer);        
        Systemoutprintln(autowire=\default\:);
        computerrun();
        
        //autodetect
        Computer computer = (Computer)acgetBean(computer);        
        Systemoutprintln(autowire=\autodetect\:);
        computerrun();
        
        //constructor
        Computer computer = (Computer)acgetBean(computer);        
        Systemoutprintln(autowire=\constructor\:);
        computerrun();
        
        //no
        Computer computer = (Computer)acgetBean(computer);        
        Systemoutprintln(autowire=\no\:);
        computerrun();
        logdebug(leaving main method);
    }
}

  applicationContextxml

   <?xml version= encoding=UTF?>
<beans xmlns=
    xmlns:xsi=instance
    xsi:schemaLocation=;beansxsd
    defaultautowire=autodetect>
    <bean id=host name=host class=comredofficeHost></bean>
    <bean id=display name=display class=comredofficeDisplay></bean>
    
    <! autowire有noconstructorbyNamebyTypedefaultautodetect幾種狀況 >
    <bean id=computer class=comredofficeComputer autowire=byName></bean>
    <bean id=computer class=comredofficeComputer autowire=byType></bean>
    <bean id=computer class=comredofficeComputer autowire=default></bean>
    <bean id=computer class=comredofficeComputer autowire=autodetect></bean>
    <! 在Computer類中必須有一個無參和有參的構造函數否則報錯 >
    <bean id=computer class=comredofficeComputer autowire=constructor>
        <constructorarg index=>
            <ref bean=host/>
        </constructorarg>
        <constructorarg index=>
            <ref bean=display/>
        </constructorarg>
    </bean>
    <bean id=computer class=comredofficeComputer autowire=no>
        <constructorarg index=>
            <ref bean=host/>
        </constructorarg>
        <constructorarg index=>
            <ref bean=display/>
        </constructorarg>
    </bean>
    
</beans>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28670.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.