Spring IoC自动装载autowire

Spring IoC自动装载autowire

自动装载是Spring提供的一种更加简便的方式来完成DI(依赖注入),不需要手动配置property,IoC容器会自动选择bean完成注入。
⭐自动装载有两种方式:

  • byName,通过属性名完成自动装载
  • byType,通过属性对应的数据类型完成自动装载
    ⭐byName的方式
  1. 创建Person实体类
package com.oyrf.entity;

import lombok.Data;

@Data
public class Person {
    private Integer Id;
    private String name;
    private Car car;
}
  1. 在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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OYBox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值