好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于@Autowierd(自动装配)的使用说明

@Autowierd(自动装配)的使用

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

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

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

?

1

2

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

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

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

?

1

2

3

4

< bean id = "Person" class = "com.spring05.pojo.Person" >

     < property name = "dog" ref = "dog" />

     < property name = "cat" ref = "cat" />

</ bean >

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

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

二、@Autowierd自动装配的使用

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

?

1

2

<!--开启注解的支持-->

< context:annotation-config />

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

?

1

2

3

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注释即可实现自动装配

?

1

2

3

4

@Autowired

private Cat cat;

@Autowired

private Dog dog;

自动装配完了之后在spring容器中注册person类时就不需要在对person类的bean添加属性注入,这边放入整个配置文件以供参考

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<? 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对象注入:

?

1

2

3

4

5

6

@Autowired

@Qualifier(value = "cat")

private Cat cat;

@Autowired

@Qualifier(value = "dog")

private Dog dog;

四、注意事项

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

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

?

1

@Autowired(required = false)

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

?

1

2

< 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启动类没有开启扫描

?

1

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

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

原文链接:https://blog.csdn.net/qq_55869124/article/details/119647409

查看更多关于基于@Autowierd(自动装配)的使用说明的详细内容...

  阅读:21次