Difference between @Bean and @Autowired
Demo01
1 @SpringBootApplication
2 public class Application {
3
4 @Autowired
5 BookingService bookingService;
6
7 public static void main(String[] args) {
8 bookingService.book("Alice", "Bob", "Carol");
9 }
10 }
Demo02
1 @SpringBootApplication
2 public class Application {
3
4 @Bean
5 BookingService bookingService() {
6 return new BookingService();
7 }
8
9 public static void main(String[] args) {
10 ApplicationContext ctx = SpringApplication.run(Application.class, args);
11 BookingService bookingService = ctx.getBean(BookingService.class);
12 bookingService.book("Alice", "Bob", "Carol");
13 }
14 }
大家可以先思考一下,上面两个demo中,生成的BookingService是否一致呢?
答案是NO!
@Bean 和@Autowired是两个用途不同的注解。
@Bean 它会告诉Spring,这是一个类的实例,先保有它的实例,然后在需要的时候再去调用这个实例。
@Autowired 它告诉Spring,请给我这个类的实例,比如,之前用@Bean注解生成的实例。
明白了吗?我的朋友。在Demo01中,@Autowired让Spring给它一个BookingService的实例,但是之前没有任何地方创建过这个实例,所以肯定会失败。在Demo02中,@Bean创建了这个实例,然后在main方法中,需要使用的地方进行调用。
将Demo01和Demo02进行整合,可以变成Demo03的样子:
1 @SpringBootApplication
2 public class Application {
3
4 @Autowired
5 BookingService bookingService;
6
7 @Bean
8 BookingService bookingService() {
9 return new BookingService();
10 }
11
12 public static void main(String[] args) {
13 bookingService.book("Alice", "Bob", "Carol");
14 }
15 }
参考链接:
https://stackoverflow.com/questions/34172888/difference-between-bean-and-autowired#:~:text=Annotating%20%40Bean%20only%20registers%20the,Object)%20from%20Spring%20Application%20Context.
Difference between @Bean and @Autowired的更多相关文章
- [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired
Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...
- @Component和@Bean以及@Autowired、@Resource
1. 有这么一个故事,从xml配置文件的bean说起 Spring用xml配置文件的时候(不知道阅读这篇文章的你用没用过,我用过一段时间,那是黑暗伤痛的回忆QQQ),一个xml配置文件里面有很多个 ...
- @Autowired & @Resource 区别 & 解读@Bean
一样 Autowired & @Resource 都可以用来Bean的注入,可以写在属性(字段)上.也可以写在setter方法上 不一样 1.来源不一样 @Autowired 由Spr ...
- Field redisTemplate in xxxxxx required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.
*************************** APPLICATION FAILED TO START *************************** Description: Fie ...
- 【spring源码系列】之【Bean的实例化】
人生需要探索的热情.坚持的勇气以及热爱生活热爱自己的力量. 1. Bean的实例化 上一篇讲述了bean的生命周期,其中第一步就涉及到了bean的实例化,本文重点分析bean实例化,先进入源码中的Ab ...
- Spring常用注解,自动扫描装配Bean
1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...
- 自动装配Bean
Spring提供了几种技巧,可以减少XML的配置数量: 自动装配(autowiring):可以减少<property>(setter注入)和<constructor-arg>( ...
- spring中bean配置和bean注入
1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...
- Spring实战3:装配bean的进阶知识
主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...
随机推荐
- 我要进大厂之大数据MapReduce知识点(1)
01 我们一起学大数据 老刘今天分享的是大数据Hadoop框架中的分布式计算MapReduce模块,MapReduce知识点有很多,大家需要耐心看,用心记,这次先分享出MapReduce的第一部分.老 ...
- php7的Opcache getshell
OPcache基础 OPcache是一种通过解析的PHP脚本预编译的字节码存放在共享内存中来避免每次加载和解析PHP脚本的开销,解析器可以直接从共享内存读取已经缓存的字节码,从而大大提高了PHP的执行 ...
- SQL注入实战新手教程
本文章仅用于网络安全交流学习,严禁用于非法用途,否则后果自负 一.如何批量找SQL注入(工具+资源都打包):http://www.liuwx.cn/post-149.html 1.SQL注入点搜索关键 ...
- (buuctf) - pwn入门部分wp - rip -- pwn1_sctf_2016
[buuctf]pwn入门 pwn学习之路引入 栈溢出引入 test_your_nc [题目链接] 注意到 Ubuntu 18, Linux系统 . nc 靶场 nc node3.buuoj.cn 2 ...
- web自动化测试--iframe切换
什么是iframe切换,我们在测试web网页过程中,可能会遇到一个网页中嵌套另一个网页的情况,如下图,就是一个ifame嵌套的例子 我们如何切换呢,别急,webdriver里有方法,可以切换到ifra ...
- locust使用小技巧(v0.13.5)
Windows下载: pip install locustio==0.13.5; 以下基于locust的0.13.5,写文章时时2019年,没想到2020年就大变样了 locust是基于python的 ...
- php bypass disable_function 命令执行 方法汇总简述
1.使用未被禁用的其他函数 exec,shell_exec,system,popen,proc_open,passthru (python_eval?perl_system ? weevely3 wi ...
- VUE:组件总结
##### 组件化 //node.js里面 require("./index.js") 模块化就是将系统功能分离成独立的功能部分的方法,一般指的是单个的某一种东西,例如js.cs ...
- spring + quartz 分布式自定义注解
相关技术 本文采用spring + quartz的方案.使用mysql作为任务的持久化,支持分布式. 自定义注解 1.启用定时任务 @Target(ElementType.TYPE) @Retenti ...
- 为什么Python中称__lt__、__gt__等为“富比较”方法
Python中基类object提供了一系列可以用于实现同类对象进行"比较"的方法,可以用于同类对象的不同实例进行比较,包括__lt__.__gt__.__le__.__ge__._ ...