Spring使用@Autowired注解实现自动装配方式

目录
  • Spring支持注解配置
    • 引入注解依赖
    • 启用注解
  • 使用@Autowired注解实现自动装配
    • 1、IOC容器配置
    • 2、实体类使用@Autowired注解注入属性
    • 3、测试结果
  • @Autowired注解的使用和注入规则
    • 1.使用在变量域上面
    • 2.@Autowired注解使用在构造器上面

Spring支持注解配置

引入注解依赖

<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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">

启用注解

<context:annotation-config/>

使用@Autowired注解实现自动装配

1、IOC容器配置

<?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.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
      <context:annotation-config/>
   <bean id="cat" class="indi.stitch.pojo.Cat"/>
   <bean id="dog" class="indi.stitch.pojo.Dog"/>
   <bean id="people" class="indi.stitch.pojo.People" />
</beans>

2、实体类使用@Autowired注解注入属性

package indi.stitch.pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    public Cat getCat() {
        return cat;
    }
    public Dog getDog() {
        return dog;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                '}';
    }
}

Cat实体类

package indi.stitch.pojo;
public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}

Dog实体类

package indi.stitch.pojo;
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}

使用@Autowired注解支持自动注入后,可以省略实体类的setter方法

3、测试结果

使用Java类库中的@Resource注解可以实现相同的效果,@Autowired和@Resource注解的区别是

  • @Autowired注解默认按byType方式实现,@Resource注解默认按byName方式实现
  • @Autowired注解在IOC容器中配置了多个相同类型的bean时,需要配合@Qualifier找到唯一bean
@Autowired
@Qualifier("cat")
private Cat cat;

@Resource注解可以配置name和type属性进行bean的注入

@Resource(name = "dog", type = Dog.class)
private Dog dog;

@Resource属性单独使用name属性后,将不会按照byType方式查找bean,@Autowired注解可以使用required属性来决定注入的属性是否允许为空

@Autowired(required = false)
 @Qualifier("cat")
 private Cat cat;

@Autowired注解的使用和注入规则

作为一个Spring开发者对@Autowired注解必定是非常了解了, 顾名思义自动装配,应该是Spring会自动将我们标记为@Autowired的元素装配好,与其猜测不如看看它的定义:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
  boolean required() default true;
}

很明显这个注解可以用到构造器,变量域,方法,注解类型和方法参数上。文档上这样描述:将一个构造器,变量域,setter方法,config方法标记为被Spring DI 工具自动装配。换句话说,在Spring创建bean的过程中,会为这个bean中标有@Autowired注解的构造器,变量域,方法和方法参数中自动注入我们需要的已经在Spring IOC容器里面的bean,,而无需我们手动完成,并且注入的bean都是单实例,也就是在两个bean中都依赖第三个bean,那么这两个bean中注入的第三个bean会是同一个bean(JVM中指向的地址相同)。

在@Autowired注解里面有一个required属性,该属性默认为true,当为true时,表示去Spring IOC中查找相应的bean,如果找不到,则会报错,如果为false时,表示去Spring IOC中查找相应的bean,如果找不到,则直接忽略,不再进行注入。

@Autowired注解的注入规则:默认按照类型进行注入,如果IOC容器中存在两个及以上的相同类型的bean时,根据bean的名称进行注入,如果没有指定名称的bean,则会报错。

可以使用@Qualifier("wheel")来使用指定id的bean,也可以在注入bean时,添加@Primary注解,优先添加一个bean,其规则如下:

如果指定添加了@Qualifier("wheel")则按照指定的bean id进行添加(优先级最高),找不到则直接报错。如果没有添加@Qualifier而添加了@Primary注解,则首先添加标注了@Primary注解的bean。当即存在@Qualifier注解也存在@Primary注解注解,则按照@Qualifier指定的bean id注入,找不到直接报错。

很多人java开发者都知道@Autowired注解,但是真正用的好的也不多(反正系统的学习Spring之前我是不知道的),那下面让我们来看一下@Autowired的用法:

1.使用在变量域上面

这个相信大家都已经清楚了,Spring会帮我们注入我们想要的bean,看下面的例子:

package it.cast.circularDependency;
@Component
public class Wheel {
}

@Component
public class Car {
    @Autowired
    private Wheel wheel2;

    public Wheel getWheel() {
        return wheel2;
    }

    public void setWheel(Wheel wheel2) {
        this.wheel2 = wheel2;
    }
}

@ComponentScan({"it.cast.circularDependency"})
public class AutowiredConfig {
}

下面进行测试,打印的结果显示可以拿到Wheel类,说明@Autowired注解在IOC容器中只有一个类型的bean时,按照类型进行注入。

@Test
    public void AutowiredConfigTest(){
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AutowiredConfig.class);
        Car bean = context.getBean(Car.class);
        System.out.println(bean.getWheel());
    }

//打印结果:
//   it.cast.circularDependency.Wheel@3eb25e1a

下面看一下当IOC容器中有两个Wheel类型的bean时的情况,改造Wheel类,增加一个属性标识用于记录向Car类中注入的哪个Wheel的bean,在AutowiredConfig配置类中添加一个bean,bean的名称默认为方法名,也就是wheel1。

@Component
public class Wheel {
    private int num = 2;   //通过包扫描的方式注入的bean的num值为2
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
}

@Component
public class Car {
    @Autowired
    private Wheel wheel3;//将变量名改成wheel3,IOC容器中Wheel类型的bean的名称只有wheel和wheel1
    public Wheel getWheel() {
        return wheel3;
    }
    public void setWheel(Wheel wheel3) {
        this.wheel3 = wheel3;
    }
} 

@Configuration
@ComponentScan({"it.cast.circularDependency"})
public class AutowiredConfig {

    @Bean
    public Wheel wheel1(){
        Wheel wheel = new Wheel();//通过配置类注入bean的方式num值为0
        wheel.setNum(0);
        return wheel;
    }
}

这时在Spring IOC中有两个Wheel类型的bean了,Car在注入Wheel类型的bean时,会根据变量名wheel3去找,也就是说会去找类型为Wheel,名称为wheel3的bean,显然是找不到的,也就会报错。

Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'car':
Unsatisfied dependency expressed through field 'wheel3';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'it.cast.circularDependency.Wheel' available:
expected single matching bean but found 2: wheel,wheel1

org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'car': Unsatisfied dependency expressed through field 'wheel3';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'it.cast.circularDependency.Wheel' available:
expected single matching bean but found 2: wheel,wheel1

上面为报错的日志打印,大致意思说的在创建名称为car的bean时,不能为变量域wheel3完成属性注入,因为找到了两个bean,分别是wheel和wheel1。

如果我们把Car中的wheel3换成wheel就可以完成注入了,而且注入的bean是通过包扫描注入IOC的bean:

@Component
public class Wheel {
    private int num = 2;   //通过包扫描的方式注入的bean的num值为2
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
}

@Component
public class Car {
    @Autowired
    private Wheel wheel;//将变量名改成wheel1,IOC容器中Wheel类型的bean的名称只有wheel和wheel1
    public Wheel getWheel() {
        return wheel;
    }
    public void setWheel(Wheel wheel3) {
        this.wheel = wheel;
    }
} 

@Configuration
@ComponentScan({"it.cast.circularDependency"})
public class AutowiredConfig {
    @Bean
    public Wheel wheel1(){
        Wheel wheel = new Wheel();//通过配置类注入bean的方式num值为0
        wheel.setNum(0);
        return wheel;
    }
}

在测试类中打印num值看看注入的是哪个bean:

@Test
    public void AutowiredConfigTest(){
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AutowiredConfig.class);

        Car bean = context.getBean(Car.class);
        System.out.println(bean.getWheel().getNum());
    }
//打印结果:
//    2

那么就验证了上面所说的注入规则:默认按照类型进行注入,如果IOC容器中存在两个及以上的相同类型的bean时,根据bean的名称进行注入,如果没有指定名称的bean,则会报错。

@Autowired注解使用在变量域中还可以解决循环依赖的问题,循环依赖问题就是A对象中注入了B对象,B对象中注入了A对象,循环依赖在面试Spring这一块的知识应该经常会被问题,关于循环依赖的问题,在后面的博客中会更新。

2.@Autowired注解使用在构造器上面

@Autowired使用在构造器上面有几条需要特别注意的点:

1.@Autowired标注在构造器上面不能解决循环依赖构造的问题

2.@Autowired可以标注在同一个类的多个构造器上面,但是required属性必须都为false,当required有一个为true时,不允许其他构造器标有@Autowired注解,即使required属性为false也不行。

@Component
public class A {
    private B b;
    private C c;

    @Autowired
    public A(B b, C c) {
        System.out.println("b=" + b + ", c=" + c);
        this.b = b;
        this.c = c;
    }
}

@Component
public class B {
}
@Component
public class C {
}

//打印结果:
//  b=it.cast.circularDependency.B@68e965f5, c=it.cast.circularDependency.C@6f27a732

@Autowired标注在构造器上面,在B创建的过程中,会去Spring IOC中拿到需要的注入的bean,完成B的创建,其实在只有一个构造器的情况中,@Autowired可以不加,因为Spring内部有自动推断构造器的能力,这个如果想了解自动推断构造器的同学可以自行百度(实在是太难了,一脸蒙蔽)。

下面看一个构造器循坏依赖的案例:

@Component
public class C {
    private B b;
    public C(B b) {
        this.b = b;
    }
}

@Component
public class B {
    private C c;
    public B(C c) {
        this.c = c;
    }
}

Spring目前不能解决构造器的循环依赖,所以在项目中使用的时候要格外注意一下,错误日志:

org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'a' defined in file
[E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\A.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'b' defined in file
[E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\B.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'c' defined in file
[E:\IdeaProjects\javaBasis\spring\target\classes\it\cast\circularDependency\C.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'b':
Requested bean is currently in creation: Is there an unresolvable circular reference?

下面看一下关于多个@Autowired标注的构造器的案例:

@Component
public class A {
    private B b;
    private C c;
    @Autowired(required = false)
    public A(B b) {
        this.b = b;
    }

    @Autowired
    public A(B b, C c) {
        System.out.println("b=" + b + ", c=" + c);
        this.b = b;
        this.c = c;
    }
}
@Component
public class B {
}
@Component
public class C {
}

上面已经说到,如果@Autowired注解的属性required为true时,不允许再出现其他构造器上面标有@Autowired注解(@Autowired注解的required默认为true,所以上面的会报错),错误日志为:

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'a': Invalid autowire-marked constructors:
[public it.cast.circularDependency.A(it.cast.circularDependency.B)].
Found constructor with 'required' Autowired annotation:
public it.cast.circularDependency.A(it.cast.circularDependency.B,it.cast.circularDependency.C)

使用下面的写法就不会出现错误了,Spring支持多个构造器有@Autowired注解,但是required属性必须都是false

@Component
public class A {
    private B b;
    private C c;

    @Autowired(required = false)
    public A(B b) {
        this.b = b;
    }

    @Autowired(required = false)
    public A(B b, C c) {
        System.out.println("b=" + b + ", c=" + c);
        this.b = b;
        this.c = c;
    }
}

@Component
public class B {
}
@Component
public class C {
}

关于@Autowired标注在方法上就不多介绍,会首先拿到方法的参数列表,然后根据上面所说的注入规则去Spring IOC中找相应的bean。

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

(0)

相关推荐

  • 彻底搞明白Spring中的自动装配和Autowired注解的使用

    一.自动装配 当Spring装配Bean属性时,有时候非常明确,就是需要将某个Bean的引用装配给指定属性.比如,如果我们的应用上下文中只有一个org.mybatis.spring.SqlSessionFactoryBean类型的Bean,那么任意一个依赖SqlSessionFactoryBean的其他Bean就是需要这个Bean.毕竟这里只有一个SqlSessionFactoryBean的Bean. 为了应对这种明确的装配场景,Spring提供了自动装配(autowiring).与其显式的装配

  • Spring注解实现自动装配过程解析

    在IOC容器中学习相关注解(常用) 1. @Autowireda.作用对象:(官网解释) 1. You can apply the @Autowired annotation to constructors: 2.you can also apply the @Autowired annotation to "traditional" setter methods: 3.You can also apply the annotation to methods with arbitrar

  • 基于Spring@Autowired注解与自动装配详谈

    1 配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss 拥有 Office 和 Car 类型的两个属性: 清单 3. Boss.java package com.baobaotao; public class Boss { private Car car; private Office office; // 省略 get/setter @Override p

  • Spring注解实现Bean自动装配示例详解

    何为自动装配 自动装配是 Spring 满足 bean 依赖的一种方式. 在使用 Spring 配置 bean 时,我们都要给配置的 bean 的属性设置一个值,如果不手动设置则都是空.而自动的好处就在于,我们不用手动去设置一个值,spring 会在上下文中自动寻找并装配合适的值. 本文主要介绍了Spring注解Bean自动装配的相关内容,下面话不多少了,来一起看看详细的介绍吧 使用须知: 1.导入约束:context约束 2.配置注解的支持: context:annotation-config

  • Spring使用@Autowired注解实现自动装配方式

    目录 Spring支持注解配置 引入注解依赖 启用注解 使用@Autowired注解实现自动装配 1.IOC容器配置 2.实体类使用@Autowired注解注入属性 3.测试结果 @Autowired注解的使用和注入规则 1.使用在变量域上面 2.@Autowired注解使用在构造器上面 Spring支持注解配置 引入注解依赖 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="ht

  • Spring使用@Autowired注解静态实例对象方式

    目录 Spring @Autowired注解静态实例对象 问题 原因 解决方案 方式一 方式二 方式三 方式四 总结 @Autowired注解和静态方法 一.业务场景 二.原理剖析 三.解决方法 1.将@Autowire加到构造方法上 2.用@PostConstruct注解 Spring @Autowired注解静态实例对象 问题 最近项目小组在重新规划工程的业务缓存,其中涉及到部分代码重构,过程中发现有些工具类中的静态方法需要依赖别的对象实例(该实例已配置在xml成Spring bean,非静

  • Spring中Bean的作用域和自动装配方式

    目录 Bean的作用域 默认配置 scope = "singleton" scope = "prototype" Bean的自动装配 通过name自动装配 通过type自动装配 Bean的作用域 Spring中bean的作用域共有singleton.prototype.request.session.application.websocket六种 其中后四种都是用在Web应用程序中的,主要介绍前两种singleton(单例)和prototype(原型) Bean的作

  • 详解Java的Spring框架下bean的自动装载方式

    Spring容器可以自动装配相互协作bean之间的关系,这有助于减少对XML配置,而无需编写一个大的基于Spring应用程序的较多的<constructor-arg>和<property>元素. 自动装配模式: 有下列自动装配模式,可用于指示Spring容器使用自动装配依赖注入.使用<bean/>元素的autowire属性为一个bean定义中指定自动装配模式. byName模式 这种模式规定由自动装配属性名称.Spring容器在外观上自动线属性设置为byName的XML

  • Spring @Autowired注解超详细示例

    目录 前言 一.依赖注入的方式 手动注入 自动装配 二.注解@Autowired的自动装配原理 @Autowired自动装配过程 实现原理 ①首先看看spring的源代码定义 ②核心逻辑在buildAutowiringMetadata中 ③InjectionMetadata类 ④ 实现注入逻辑 ⑤调用InjectionMetadata中的公共inject ⑥遍历调用protect的inject方法 前言 说明:我们今天要分享的是依赖注入(DI)通过自动装配的注解方式@Autowird注解方法的作

  • Java注解机制之Spring自动装配实现原理详解

    Java中使用注解的情况主要在SpringMVC(Spring Boot等),注解实际上相当于一种标记语言,它允许你在运行时动态地对拥有该标记的成员进行操作.注意:spring框架默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean>标签的autowire属性. 自动装配属性有6个值可选,分别代表不同的含义: byName ->从Spring环境中获取目标对象时,目标对象中的属性会根据名称在整个Spring环境中查找<bean>标签的id属性值.如果

随机推荐