@Autowired详解

本文详细介绍了@Autowired注解在Spring框架中的作用,包括自动创建bean、属性注入、byType和byName注入策略,以及其在静态变量和静态方法上的限制。
摘要由CSDN通过智能技术生成

请直接看原文:

@Autowired注解详解——超详细易懂-CSDN博客

-------------------------------------------------------------------------------------------------------------------------------- 

一.@Autowired详解

要搞明白@Autowired注解就是要了解它是什么?有什么作用?怎么用?为什么?

  • 首先了解一下IOC操作Bean管理,bean管理是指(1)spring创建对象 (2)spring注入属性。当我们在将一个类上标注@Service或者@Controller或@Component或@Repository注解之后,spring的组件扫描就会自动发现它,并且会将其初始化为spring应用上下文中的bean。 而且初始化是根据无参构造函数。先看代码来体会一下这个注解的作用,测试代码如下:(@Data注解是由Lombok库提供的,会生成getter、setter以及equals()、hashCode()、toString()等方法)
@Data
@Service
public class AutoWiredBean {
    private int id;
    private String name;

    public AutoWiredBean(){
        System.out.println("无参构造函数");
    }

    public AutoWiredBean(int id, String name) {
        this.id = id;
        this.name = name;
        System.out.println("有参构造函数");
    }
}

在springboot项目的测试类中进行测试,代码如下

@SpringBootTest
@RunWith(SpringRunner.class)
class Springboot02WebApplicationTests {


    private AutoWiredBean autoWiredBean;

    @Autowired
    public Springboot02WebApplicationTests (AutoWiredBean autoWiredBean){
        this.autoWiredBean = autoWiredBean;
    }

    @Test
    void contextLoads() {
    	System.out.println(autoWiredBean);
        System.out.println(autoWiredBean.getId());  //0
        System.out.println(autoWiredBean.getName());    //null
       
    }

}

控制台输出的结果如下:在这里插入图片描述

将下面代码注释了在运行

 /* @Autowired
    public Springboot02WebApplicationTests (AutoWiredBean autoWiredBean){
        this.autoWiredBean = autoWiredBean;
    }*/

输出结果如下:在这里插入图片描述
从这我们可以看到

1.无论有没有使用AutoWiredBean 类,它都被spring通过无参构造函数初始化了。当将被使用时才会创建。就对应了Spring在启动时,默认会立即调用单实例bean的空参构造方法创建bean的对象,并加载到Spring容器中。

2.不用@Autowired的private AutoWiredBean autoWiredBean值为null; 用了@Autowired的private AutoWiredBean autoWiredBean值为真实存在的AutoWiredBean类的对象;


二.有什么用

给对象类型的属性赋值(就是将本来为null的private AutoWiredBean autoWiredBean值赋值为真实存在的AutoWiredBean类的对象),可以用在成员变量、成员方法、构造方法上.

@Autowired 注解注入时首先根据byType注入,当接口存在多个实现类且使用@Service注解的默认bean名字时,根据byName注入。

@Service注解默认bean名字是小写开头的非限定(non-qualified)类名.

如果出现多个实现类可以通过 @Service("userServiceA") 来指定bean名,此时使用@Autowired需要配合@Qualifier("userServiceA")使用.

三.注意:

@Autowired 注解不能用在静态变量和静态方法上,  是因为静态变量和静态方法属于类加载期间,

而@Autowired属于spring上下文容器期间,  spring上下文容器期间只是创建类的对象,并不能对属于类的静态变量和静态方法进行操作, 因此,@Autowired加在静态变量和静态方法是无效的,并不能完成赋值功能.

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
@Autowired注解用于自动装配Spring容器中的Bean。当标注在构造函数上时,会将符合类型的Bean自动注入到构造函数中作为参数。如果属性是一个接口类型,它会根据类型找到对应的实现类进行注入。如果有多个实现类,可以结合@Qualifier注解指定具体要注入的实现类的名称。 需要注意的是,在BeanPostProcessor类和BeanFactoryPostProcessor类中无法使用@Autowired注解。因为@Autowired注解的收集工作是由BeanPostProcessor类完成的,而BeanFactoryPostProcessor类的调用时机先于BeanPostProcessor类,所以无法使用@Autowired注解在这两个类中。 另外,如果想禁用某些特定构造函数的自动装配,可以使用注释将其注释掉。比如在代码中使用/* */将构造函数注释掉,可以阻止@Autowired注解的自动注入。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [@Autowired注解详解——超详细易懂](https://blog.csdn.net/weixin_45755816/article/details/118654961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [@Autowired注解详解](https://blog.csdn.net/qq1309664161/article/details/119293360)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值