@Autowierd自动装配的使用 基于@Autowierd(自动装配)的使用教程

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

@Autowierd自动装配的使用 基于@Autowierd(自动装配)的使用教程

熊熊熊熊可爱   2021-08-14 我要评论
想了解基于@Autowierd(自动装配)的使用教程的相关内容吗,熊熊熊熊可爱在本文为您仔细讲解@Autowierd自动装配的使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:@Autowierd装配,自动装配,下面大家一起来学习吧。

@Autowierd(自动装配)的使用

@Autowired 是一个注释,它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。

一、介绍@Autowierd自动装配之前我们需要先了解何为装配?

首先我们来看最原生态的装配,以一个人分别养了猫和狗为例,我们先分别为猫和狗进行实例化:

    <bean id="cat" class="com.spring05.pojo.Cat"/>
    <bean id="dog" class="com.spring05.pojo.Dog"/>

由于person类的属性中带有猫和狗,所以我们需要将猫和狗的实体类注入人的实体类中:

    <bean id = "Person" class="com.spring05.pojo.Person">
        <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
    </bean>

以上就是装配,所谓的属性注入

但是我们知道,如果是手动注入的属性的话,一旦属性数量多的话会显得很繁琐,这时候自动装配的作用就体现出来了

二、@Autowierd自动装配的使用

第一步,使用@Autowierd注释需要在配置文件中开启注解支持

        <!--开启注解的支持-->
        <context:annotation-config/>

但是相应的需要在配置文件中加入context约束:

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

接下来就是注解的使用了,@Autowierd注释的使用只需要在Person类中的属性上加上一个@Autowierd注释即可实现自动装配

    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;

自动装配完了之后在spring容器中注册person类时就不需要在对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/context
        http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
 
        <!--开启注解的支持-->
        <context:annotation-config/>
    <bean id="cat" class="com.spring05.pojo.Cat"/>
    <bean id="dog" class="com.spring05.pojo.Dog"/>
    <bean id="Person" class="com.spring05.pojo.Person"/>
</beans>

除了@Autowierd之外还需要介绍@Resource注释,@Resource注释与@Autowierd功能相同,@Resource甚至包括了@Autowierd

三、使用注解@Autowierd的"搭档"@Qualifier

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

    @Autowired
    @Qualifier(value = "cat")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog")
    private Dog dog;

四、注意事项

1、使用Autowired我们可以省略set方法,但是使用注解的前提是装配的属性必须在IOC容器中存在,且符合名字byname

2、如果定义了@Autowired的required属性为false,说明这个对象可以为空,否则不允许为空:

@Autowired(required = false)

3、不仅仅只有通过注释可以自动装配,还可以通过ByName和ByType来自动装配:

    <bean id="Person" class="com.spring05.pojo.Person" autowire="byType"/>
    <bean id="Person" class="com.spring05.pojo.Person" autowire="byName"/>

SpringBoot的Autowierd失败

通常是以下几种可能:

1.没有加@Service注解,或者是这个bean没有放在标注了@Configuration这个注解的类下。

2.SpringBoot启动类没有开启扫描

@ComponentScan(value = {"com.bihang"})

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们