spring(10)——使用注解实现bean的自动装配@Autowired、@Qualifier、@Resource

1.使用注解需要先引入支持注解的环境

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd">
  1. 上面设计context的三行则是为了支持注解所引入的。

2.使用注解需要写一个支持注解的标签

<context:annotation-config/>
  1. 写了这个标签后,才能成功的使用注解进行注解装配

3.准备好几个bean用于被装配(beans的总代码)

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd">
    
    <bean id="cat1" class="com.lixv.entity.Cat"/>
    <bean id="cat2" class="com.lixv.entity.Cat"/>
    <bean id="dog1" class="com.lixv.entity.Dog"/>
    <bean id="dog2" class="com.lixv.entity.Dog"/>
    <!-- 注解的支持 -->
    <context:annotation-config/>
    <bean id="people" class="com.lixv.entity.People" p:name="LLLLL"/>
</beans>

4. 在实体类中使用注解

4.1通过@Autowired注解实现bean的自动装配

package com.lixv.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.Resource;

public class People {
    private String name;
    @Autowired(required = false)
    @Qualifier(value = "cat1")
    private Cat cat;
    @Autowired(required = false)
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = 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;
    }

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

  1. @Autowired注解的作用是,将这个类自动装配
  2. @Autowired注解有一个require属性,默认值为true,可手动将其改为false。当@Autowired(required = false)的时候,这个属性在beans中找不到bean来装配也不会报错,否则会报错。
  3. @Autowired会首先寻找bean中id与属性名相同的bean进行装配,如果没有寻找到则再根据类型去装配,若这个时候有多个这种类型的bean,则会报错。
  4. 有多个这种类型的bean导致报错的时候,我们可以通过@Qualifier(value = "cat1")来协助自动装配,通过寻找bean的id与@Qualifier注解的value属性值相同的bean来进行装配

4.2通过@Resource注解实现bean的自动装配

package com.lixv.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.Resource;

public class People {
    private String name;
    @Resource(name = "cat1")
    private Cat cat;
    @Resource(name = "dog1")
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = 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;
    }

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

  1. @Resource相当于整合了@Autowired和@Qualifier注解。
  2. @Resource注解没有@Autowired的require属性
  3. @Resource注解有一个name属性相当于@Qualifier的value属性。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

木子 旭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值