1. 程式人生 > 其它 >3.spring基於xml的自動裝配

3.spring基於xml的自動裝配

1.autowire="byType"
    ----->以屬性的型別作為查詢依據去容器中找到這個元件
    
2.autowire="byName"
    private Car car;
        以屬性名(car)作為id去容器中找到這個元件,給他賦值;如果找不到就裝配null;
        car=ioc.getBean("car");
    
3.autowire="constructor"
    靠構造器注入
    
4.autowire="default"或者autowire="no"(預設:不自動裝配)
示例:

1.autowire="byType":以屬性的型別來查詢

前提實體類:
    public class Person {
        private String name;
        private Integer age;
        private Car car;
        private List<Book> bookList;
        get/set方法
        toString方法
        ...
    }
    public class Book {
        private String bookName;
        private Integer price;
        get/set方法
        toString方法
        ...
    }
    public class Car {
        private String carName;
        private Integer price;
        get/set方法
        toString方法
        ...
    }
spring的配置檔案寫法:person中car的自動注入
    <bean id="car" class="entity.Car">
        <property name="carName" value="寶馬"></property>
        <property name="price" value="3000"></property>
    </bean>
    <bean id="person" class="entity.Person" autowire="byType"></bean>---------->以屬性的型別去容器查詢元件:(person有car的屬性)
結論:
    autowire="byType"相當於ioc.getBean(Car.class),前提:spring容器中只能有一個Car的元件
    如果有兩個:比如
         <bean id="car1" class="entity.Car">
            <property name="carName" value="寶馬"></property>
            <property name="price" value="3000"></property>
        </bean>
         <bean id="car2" class="entity.Car">
            <property name="carName" value="寶馬"></property>
            <property name="price" value="3000"></property>
        </bean>
        這時靠autowire="byType"會報錯:
        Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: 
        Error creating bean with name 'person' defined in class path resource [ioc.xml]: 
        Unsatisfied dependency expressed through bean property 'car'; 
        nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
        No qualifying bean of type 'entity.Car' available: expected single matching bean but found 2: car,car1

 

2.autowire="byName":按照名字

person{ private Car car; 以屬性名(car)作為id去容器中查詢這個元件給他賦值;如果找不到就裝配null 相當於:car=ioc.getBean("car"); }
1.person實體類
    public class Person {
        ...
        private Car car;------>通過(car)作為id去容器中查詢元件
        ...
    }
2.spring的配置檔案中‘
    <bean id="car" class="entity.Car">
            <property name="carName" value="寶馬"></property>
            <property name="price" value="3000"></property>
    </bean>
    <bean id="person" class="entity.Person" autowire="byName"></bean>---------->byName:按照id去查詢

3.如果此時
    <bean id="car1" class="entity.Car">
            <property name="carName" value="寶馬"></property>
            <property name="price" value="3000"></property>
    </bean>
    會根據id無法找到該元件,裝配到null

 

3.autowire="constructor"

按照構造器進行賦值: 1>先按照有參構造器的引數型別進行裝配(成功就賦值);沒有就直接為元件裝配null即可 2>如果按照型別找到多個;引數的名作為id繼續裝配;找到就匹配,找不到就null 3>不會報錯
1.person實體類
    public class Person {
        ...
        private Car car;------>通過(car)作為id去容器中查詢元件
        ...
        public Person() {-------------------------------------------->必須要有無參構造器,要不會報錯
            System.out.println("person的無參構造器");
        }
        public Person(Car car) {------------------------------------->只有car一個屬性的構造器
            System.out.println("person的car構造器");
            this.car = car;
        }
    }
2.spring的配置檔案
    2.1按照型別可以查詢到多個元件,且按照id(car)沒有找到對應元件
        <bean id="car1" class="entity.Car">---------------------------->id為:car1
            <property name="carName" value="寶馬"></property>
            <property name="price" value="3000"></property>
        </bean>
        <bean id="car2" class="entity.Car">----------------------------->id為:car2
                <property name="carName" value="五菱"></property>
                <property name="price" value="200"></property>
        </bean>
        <bean id="person" class="entity.Person" autowire="constructor"></bean>----->1.先按照型別查詢,找到多個,按照id找不到,裝配為null
        獲取person的輸出:Person{name='null', age=null, car=null, bookList=null}
   
     2.2按照型別找到多個,但是根據id(car)可以查詢到對應元件
        <bean id="car" class="entity.Car">---------------------------->id為:car,和person中的car屬性相同
            <property name="carName" value="寶馬"></property>
            <property name="price" value="3000"></property>
        </bean>
        <bean id="car2" class="entity.Car">----------------------------->id為:car2
                <property name="carName" value="五菱"></property>
                <property name="price" value="200"></property>
        </bean>
        <bean id="person" class="entity.Person" autowire="constructor"></bean>----->1,按照型別查詢到多個,但是根據id(car)在容器中找到一個,注入
        獲取person輸出:Person{name='null', age=null, car=Car{carName='寶馬', price=3000}, bookList=null}
    
    2.3按照型別找到一個()
        <bean id="car2" class="entity.Car">----------------------------->id為:car2
                <property name="carName" value="五菱"></property>
                <property name="price" value="200"></property>
        </bean>
        <bean id="person" class="entity.Person" autowire="constructor"></bean>---->按照型別查詢到一個,直接注入
        獲取person輸出:Person{name='null', age=null, car=Car{carName='五菱', price=200}, bookList=null}

4.autowire="byType"的使用示例

實體類:
    public class Person {
        ...
        private List<Book> bookList;--->book的list集合
        ...
    }
    public class Book {
        private String bookName;
        private Integer price;
    }
spring的配置檔案
    <bean id="book1" class="entity.Book">
        <property name="bookName" value="java"></property>
        <property name="price" value="15"></property>
    </bean>
    <bean id="book2" class="entity.Book">
                <property name="bookName" value="c++"></property>
            <property name="price" value="25"></property>
    </bean>
    <bean id="person" class="entity.Person" autowire="byType"></bean>------->按照型別裝配
    獲取輸出:
        Person{name='null', age=null, car=null, bookList=[Book{bookName='java', price=15}, Book{bookName='c++', price=25}]}
    結論:perosn按照型別裝配,perosn中有個book的list集合,容器會把容器中所有的book元件封裝到perosn中的List<Book>