Spring IoC自动装载autowire
自动装载是Spring提供的一种更加简便的方式来完成DI(依赖注入),不需要手动配置property,IoC容器会自动选择bean完成注入。
⭐自动装载有两种方式:
- byName,通过属性名完成自动装载
- byType,通过属性对应的数据类型完成自动装载
⭐byName的方式
- 创建Person实体类
package com.oyrf.entity;
import lombok.Data;
@Data
public class Person {
private Integer Id;
private String name;
private Car car;
}
- 在spring.xml中配置Car和Person对应的bean,并且通过自动装载完成依赖注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
xmlns:p="http://www.springframework.org/schema/p">
<bean id="person" class="com.oyrf.entity.Person" autowire="byName">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.oyrf.entity.Car">
<constructor-arg name="num" value="1"></constructor-arg>
<constructor-arg name="brand" value="宝马"></constructor-arg>
</bean>
</beans>
⭐byType的方式(把id改成car2,不是根据名字去取了,而是根据类型)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
xmlns:p="http://www.springframework.org/schema/p">
<bean id="person" class="com.oyrf.entity.Person" autowire="byType">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car2" class="com.oyrf.entity.Car">
<constructor-arg name="num" value="1"></constructor-arg>
<constructor-arg name="brand" value="宝马"></constructor-arg>
</bean>
</beans>
⭐注意:
使用byType进行自动装载时,必须保证IoC中只有一个符合条件的bean,否则会抛出异常。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
xmlns:p="http://www.springframework.org/schema/p">
<bean id="person" class="com.oyrf.entity.Person" autowire="byType">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.oyrf.entity.Car">
<constructor-arg name="num" value="1"></constructor-arg>
<constructor-arg name="brand" value="宝马"></constructor-arg>
</bean>
<bean id="car2" class="com.oyrf.entity.Car">
<constructor-arg name="num" value="2"></constructor-arg>
<constructor-arg name="brand" value="奔驰"></constructor-arg>
</bean>
</beans>
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.oyrf.entity.Car' available: expected single matching bean but found 2: car,car2