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的更多相关文章

  1. [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired

    Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...

  2. @Component和@Bean以及@Autowired、@Resource

    1. 有这么一个故事,从xml配置文件的bean说起   Spring用xml配置文件的时候(不知道阅读这篇文章的你用没用过,我用过一段时间,那是黑暗伤痛的回忆QQQ),一个xml配置文件里面有很多个 ...

  3. @Autowired & @Resource 区别 & 解读@Bean

    一样     Autowired & @Resource 都可以用来Bean的注入,可以写在属性(字段)上.也可以写在setter方法上 不一样 1.来源不一样 @Autowired 由Spr ...

  4. 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 ...

  5. 【spring源码系列】之【Bean的实例化】

    人生需要探索的热情.坚持的勇气以及热爱生活热爱自己的力量. 1. Bean的实例化 上一篇讲述了bean的生命周期,其中第一步就涉及到了bean的实例化,本文重点分析bean实例化,先进入源码中的Ab ...

  6. Spring常用注解,自动扫描装配Bean

    1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...

  7. 自动装配Bean

    Spring提供了几种技巧,可以减少XML的配置数量: 自动装配(autowiring):可以减少<property>(setter注入)和<constructor-arg>( ...

  8. spring中bean配置和bean注入

    1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...

  9. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

随机推荐

  1. javascript布局转换

    javascript布局转换是将原有的浮动布局通过JS里面的javascript属性 offsetLeft 和offsetTop默认值来给元素设置绝对定位. 原来的:缺点--我们想给网页中的图片添加拖 ...

  2. Maximum execution time of 30 seconds exceeded in

    在执行一次php脚本的时候,遇到了这样的报错,经过c Maximum execution time of 30 seconds exceeded in 翻译过来就是:执行时间超过了30秒最长执行时间: ...

  3. webug第二关:从图片中你能找到什么?

    第二关:从图片中你能找到什么? 记事本打开发现提示 binwalk,发现压缩包rar

  4. 精益求精!Spring Boot 知识点全面回顾,带你重新细读源码!

    约定优于配置 Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-bas ...

  5. 闭关苦修半年,最终靠着这份面试题,我成功拿到了阿里的offer!

    前言 我大概我是从去年12月份开始看书学习,到今年的6月份,一直学到看大家的面经基本上百分之90以上都会,我就在5月份开始投简历,边面试边补充基础知识等.也是有些辛苦.终于是在前不久拿到了阿里的off ...

  6. Docker这么火爆。章节一:带你详尽了解Docker容器的介绍及使用

    前言 很多小伙伴可能在工作中都听说过Docker,但是实际工作中却没有使用过,听得多了,也对Docker内心有一种很深切的想了解,但是因为各种原因而不知道如何去了解而发愁,不要急,这篇文章带你认识Do ...

  7. 借助Mac自带的强大的搜索功能,如何快速搜索打开Tuxera Disk Manager

    现在很多小伙伴们在遇到Mac读写NTFS格式硬盘问题的时候,都会选择使用Tuxera NTFS这个磁盘读写工具.因为这款读写工具不仅可以帮助我们进行读写工作,还具有一个磁盘管理工具Disk Manag ...

  8. Math对象-JavaScript

    Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法.Math 不是一个函数对象. Math 用于 Number 类型. Math 的所有属性与方法都是静态的. 属性 Math.E 欧拉常数 ...

  9. DRAM三种刷新方式(转载)

    设DRAM中电容的电荷每2ms就会丢失,所以2ms内必须对其补充.补充电荷是按行来进行的,为了[全部]内存都能保住电荷,必须对[所有]的行都得补充. 假设刷新1行的时间为0.5μs(刷新时间是等于存取 ...

  10. Python to Exe By Install PyInstaller on Win7-64bit

    本文主要记录为史振华在尝试转换PY文件为EXE文件过程中各种疑惑和最终解决方法,尝试了PYTHON 2.7/3.5/3.6及其相关依赖pywin32-222.win32/pywin32-222.win ...