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>