注解 java autowired_Spring注解之@Autowired

前言

说起Spring的@Autowired注解,想必大家已经熟悉的不能再熟悉了。本文就针对此最常用的注解,梳理一下它的功能和原理,争取从源码的角度将此注解讲通,如有写的不准确的地方,欢迎各位园友拍砖。

注:此篇博文基于Spring5.1.10.RELEASE,SpringBoot2.1.9.RELEASE

正文

首先看一下@Autowired注解的源码

1 packageorg.springframework.beans.factory.annotation;2

3 importjava.lang.annotation.Documented;4 importjava.lang.annotation.ElementType;5 importjava.lang.annotation.Retention;6 importjava.lang.annotation.RetentionPolicy;7 importjava.lang.annotation.Target;8

9 @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})10 @Retention(RetentionPolicy.RUNTIME)11 @Documented12 public @interfaceAutowired {13

14 /**

15 * Declares whether the annotated dependency is required.16 *

Defaults to {@codetrue}.17 */

18 boolean required() default true;19

20 }

可以看到,此注解运行时生效,且适用范围很广,从构造器,到方法,到参数、属性、注解,都可以加上@Autowired注解。它还有一个属性required,此属性用于控制如果找不到要依赖注入的对象时是否报错,默认true即默认找不到要注入的对象时会报错。下面我们慢慢梳理,将@Autowired的使用场景一一罗列出来。

一、普通用法,依赖注入成员变量

下面将要用的测试类贴出来,首先是接口

1 packagecom.mybokeyuan.springAutowiredDemo;2

3 public interfaceIndexInterface {4 }

再就是两个子类

1 packagecom.mybokeyuan.springAutowiredDemo;2

3 importorg.springframework.stereotype.Component;4

5 @Component6 public class IndexService implementsIndexInterface{7

8 }

1 packagecom.mybokeyuan.springAutowiredDemo;2

3 importorg.springframework.stereotype.Component;4

5 @Component6 public class OtherIndexService implementsIndexInterface{7

8 }

核心类(后续的各种场景演示均只基于此类进行改动)

1 packagecom.mybokeyuan.springAutowiredDemo;2

3 importorg.springframework.beans.factory.annotation.Autowired;4 importorg.springframework.stereotype.Component;5

6 @Component7 public classMyService {8

9 @Autowired10 privateIndexService indexService;11

12 }

扫描配置类

1 packagecom.mybokeyuan.springAutowiredDemo;2

3 importorg.springframework.context.annotation.ComponentScan;4 importorg.springframework.context.annotation.Configuration;5

6 @Configuration7 @ComponentScan("com.mybokeyuan.springAutowiredDemo")8 public classAutowiredConfig {9 }

main方法类

1 packagecom.mybokeyuan.springAutowiredDemo.client;2

3 importcom.mybokeyuan.springAutowiredDemo.AutowiredConfig;4 importcom.mybokeyuan.springAutowiredDemo.MyService;5 importorg.springframework.context.annotation.AnnotationConfigApplicationContext;6

7 public classDemoClientTest {8 public static voidmain(String[] args) {9 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AutowiredConfig.class);10 System.out.println(ac.getBean(MyService.class));11

12 }13 }

运行main方法时,如果在第10行打了断点,会看到MyService中的indexService属性已经依赖注入了值,这是我们最常使用@Autowired注解的场景。

二、依赖注入方法

MyService类如下所示

1 packagecom.mybokeyuan.springAutowiredDemo;2

3 importorg.springframework.beans.factory.annotation.Autowired;4 importorg.springframework.context.ApplicationContext;5 importorg.springframework.stereotype.Component;6

7 importjava.util.List;8

9 @Component10 public classMyService {11

12 privateIndexService indexService;13

14 @Autowired15 public voidgetIndexInterf (IndexService index) {16 indexService =index;17 }18

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值