bean的自动装配

7、bean的自动装配

  • 自动装配是Spring满足bean依赖一种方式!
  • Spring会在上下文中自动寻找,并自动给bean装配属性!

在Spring中有三种装配的方式

  1. 在xml中显示的配置
  2. 在java中显示配置
  3. 隐式的自动装配bean【重要】

7.1、测试

环境搭建:一个人有两个宠物

7.2、ByName自动装配

     <!--
    byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id!
    -->
    <bean id="people" class="com.maple.pojo.People" autowire="byName">
        <property name="name" value="maple"/>
    </bean>

7.3、ByType自动装配

    <bean  class="com.maple.pojo.Cat"/>
    <bean  class="com.maple.pojo.Dog"/>
    <!--
    byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id!
    byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!
    -->
    <bean id="people" class="com.maple.pojo.People" autowire="byType">
        <property name="name" value="maple"/>
    </bean>

小结:

  • ByName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性set方法一致!
  • ByType的时候,需要保证所有bean的Class唯一,并且这个bean需要和自动注入的属性的类型一致!

7.4、使用注解实现自动装配

jdk1.5之后支持注解,spring2.5后就支持了

The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.

要使用注解须知:

  1. 导入约束 context约束
  2. 配置注解的支持 :context:annotation-config/【重点】
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

@Autowired

在属性上使用即可!也可以在set方法是使用!

使用@Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(spring)容器中存在,并且符合byname!

科普:

@Nullable  字段标记了这个注解,说明这个字段可以为null
public @interface Autowired {
    boolean required() default true;
}

测试代码:

public class People {

    //如果显示的标识了required = false说明了字段可以为null
    @Autowired(required = false)
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成时,我们可以使用@Qualifier(value = “xxx”)去配合@Autowired使用,指定一个唯一的bean注入!

public class People {

    //如果显示的标识了required = false说明了字段可以为null
    @Autowired(required = false)
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog")
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

@Resource注解

public class People {

    @Resource
    private Cat cat;
    @Resource
    private Dog dog;
    private String name;
}

小结:

@Autowired和@Resource的区别:

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired通过ByType方式实现
  • @Resource默认通过byName方式实现,如果找不到名字,则通过ByType实现!如果二个都找不到就报错【常用】
  • 执行顺序不同:@Autowired通过ByType方式实现。@Resource默认通过byName方式实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值