@Autowired注解使用场景
- 构造函数注入:当
@Autowired
注解应用于构造函数时,Spring 容器会在创建 bean 时自动调用该构造函数,并注入所有需要的依赖。 - 方法注入:当
@Autowired
注解应用于方法时,Spring 容器会在创建 bean 后调用该方法,并注入所有需要的依赖。 - 字段注入:当
@Autowired
注解应用于字段时,Spring 容器会在创建 bean 时自动注入该字段。 - 属性 setter 方法注入:当
@Autowired
注解应用于 setter 方法时,Spring 容器会在创建 bean 后调用该 setter 方法,并注入所有需要的依赖。 - 当有多个Bean符合注入条件时,可以使用
@Qualifier
注解与@Autowired
结合使用,来指定具体要注入的Bean。这样可以解决自动装配时的歧义问题。
@Autowired测试示例代码
示例代码 一
AutowiredDemoService类
package com.yang.SpringTest.annotation.autowiredLearn;
import org.springframework.stereotype.Service;
/**
* <p>Service类</p>
*
* @author By: chengxuyuanshitang
* Package com.yang.SpringTest.annotation.autowiredLearn
* Ceate Time 2024-03-28 13:53
*/
@Service
public class AutowiredDemoService {
public void test () {
System.out.println ("this is AutowiredDemoService test");
}
}
AutowiredDemoController类
package com.yang.SpringTest.annotation.autowiredLearn;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* <p>Controller类</p>
*
* @author By: chengxuyuanshitang
* Package com.yang.SpringTest.annotation.autowiredLearn
* Ceate Time 2024-03-28 13:53
*/
@ToString
@Controller
public class AutowiredDemoController {
@Autowired
private AutowiredDemoService autowiredDemoService;
public void test () {
System.out.println ("AutowiredDemoController.test");
autowiredDemoService.test ();
}
}
AutowiredDemoConfig配置类
package com.yang.SpringTest.annotation.autowiredLearn;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* <p>AutowiredDemoConfig配置类</p>
*
* @author By: chengxuyuanshitang
* Package com.yang.SpringTest.annotation.autowiredLearn
* Ceate Time 2024-03-28 13:56
*/
@Configuration
@ComponentScan(value = {"com.yang.SpringTest.annotation.autowiredLearn"})
public class AutowiredDemoConfig {
}
AutowiredDemoTest测试类
package com.yang.SpringTest.annotation.autowiredLearn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* <p>AutowiredDemoTest测试类</p>
*
* @author By: chengxuyuanshitang
* Package com.yang.SpringTest.annotation.autowiredLearn
* Ceate Time 2024-03-28 13:57
*/
@Slf4j
public class AutowiredDemoTest {
public static void main (String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (AutowiredDemoConfig.class);
AutowiredDemoService autowiredDemoService = context.getBean (AutowiredDemoService.class);
log.info ("autowiredDemoService is : [ {} ]", autowiredDemoService);
AutowiredDemoController autowiredDemoController = context.getBean (AutowiredDemoController.class);
log.info ("autowiredDemoController is : [ {} ]", autowiredDemoController);
autowiredDemoController.test ();
}
}
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/287712eba45b16b1448fc0daae8bec78.png)
![img](https://img-blog.csdnimg.cn/img_convert/1af8d38c782c694af9b2eb457a46b23b.png)
![img](https://img-blog.csdnimg.cn/img_convert/a65677f92744c4062ba28b4cc0263ce0.png)
![img](https://img-blog.csdnimg.cn/img_convert/5a92a5e387d2a84ae0d44c526b996530.png)
![img](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)
![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)**
学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**
**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)**
![](https://img-blog.csdnimg.cn/img_convert/a5c7d2fe79efa0b1e81813876cfcc284.jpeg)