当前位置:网站首页>@Bean注解详解
@Bean注解详解
2022-07-30 15:48:00 【头顶凉凉先生丶】
@Bean注解详解
1、什么是@Bean
Spring中最重要的两大特性之一Ioc【控制反转】,也就是我们以后将创建对象的机会全都交给Spring的容器进行处理。具体Spring可以见如下图Spring的基础架构图。
由于不断简化开发流程所以Springboot走进了我们的视野,那么基于注解式开发的热潮逐渐覆盖了基于XML纯配置的开发,而作为Spring中最核心的bean当然也能够使用注解的方式进行表示。
Spring的@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IOC容器中。
SpringIOC 容器管理一个或者多个bean,这些bean都需要在@Configuration注解下进行创建,在一个方法上使用@Bean注解就表明这个方法需要交给Spring进行管理。
2、@Bean 基本构成及其使用
@Bean不仅可以作用在方法上,也可以作用在注解类型上,在运行时提供注册。
#源码如下
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
@AliasFor("name")
String[] value() default {};
@AliasFor("value")
String[] name() default {};
/** @deprecated */
@Deprecated
Autowire autowire() default Autowire.NO;
boolean autowireCandidate() default true;
String initMethod() default "";
String destroyMethod() default "(inferred)";
}
@AliasFor("name") String[] value() default {}; # name属性的别名,在不需要其他属性时使用,也就是说value 就是默认值
@AliasFor("value") String[] name() default {}; #bean 的名称,或多个名称,主要的bean的名称加别名。如果未指定,则bean的名称是带注解方法的名称。如果指定了,方法的名称就会忽略,如果没有其他属性声明的话,bean的名称和别名可能通过value属性配置
Autowire autowire() default Autowire.NO; # 常量,表示根本没有自动装配。 # NO(AutowireCapableBeanFactory.AUTOWIRE_NO), # 常量,通过名称进行自动装配 # BY_NAME(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME), # 常量,通过类型进行自动装配 # BY_TYPE(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE); #表示表示自动装配的类型,返回一个Autowire类型的枚举。内容为bean是否应该使用setter注入由Spring容器自动注入其依赖项,DI的体现。
String initMethod() default ""; #这个可选择的方法在bean实例化的时候调用
String destroyMethod() default "(inferred)"; #bean销毁的时候进行回调调用
初始化方法在得到Bean的实例的时候就会被调用,销毁方法在容器销毁或者容器关闭的时候会被调用。
3、常见问题:
3.1、Spring的@Bean只能放在@Configuration里面吗?
只要是@Component注解修饰的类里面都可以定义@Bean,并且都可以注册到Spring容器里面。其实不仅仅是@Component,只要是被@Component修饰的注解同样也可以定义@Bean,比如:@Repository、@Service、@Controller @Configuration,甚至是接口的default方法上的@Bean也可以被扫描加入到Spring容器里面
3.2、@Component 和 @Bean 的区别是什么?
1、作用对象不同: @Component 注解作用于类,而@Bean注解作用于方法。
2、@Component通常是通过类路径扫描来自动侦测以及自动装配到Spring容器中(我们可以使用 @ComponentScan 注解定义要扫描的路径从中找出标识了需要装配的类自动装配到 Spring 的 bean 容器中)。@Bean 注解通常是我们在标有该注解的方法中定义产生这个 bean,@Bean告诉了Spring这是某个类的示例,当我需要用它的时候还给我。
3、@Bean 注解比 Component 注解的自定义性更强,而且很多地方我们只能通过 @Bean 注解来注册bean。比如当我们引用第三方库中的类需要装配到 Spring容器时,则只能通过 @Bean来实现。
边栏推荐
- 路遇又一个流量风口,民宿长期向好的逻辑选对了吗
- yarn安装详细教程说明、升级教程、修改yarn的全局和缓存目录、yarn基本命令
- Applicable scenarios of TiDB tools
- Golang分布式应用定时任务如何实现
- 围绕用户思维,木鸟与途家如何实现乡村民宿下的用户运营
- [flutter]什么是MaterialApp和Material design
- 经典实例分割模型Mask RCNN原理与测试
- Back waves are coming!Ali produced the "second generation" container technical manual and brain map, which is too fragrant
- flask获取post请求参数
- Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs
猜你喜欢
yarn安装详细教程说明、升级教程、修改yarn的全局和缓存目录、yarn基本命令
加密生活,Web3 项目合伙人的一天
AI遮天传 DL-CNN
Why is there no data reported when the application is connected to Huawei Analytics in the application debugging mode?
【AGC】开放式测试示例
【HMS core】【FAQ】push kit、AR Engine、广告服务、扫描服务典型问题合集2
70行代码撸一个桌面自动翻译神器
How to implement timing tasks for distributed applications in Golang
完美绕开CRC32检测的无痕hook
【HMS core】【FAQ】push kit、分析服务、视频编辑服务典型问题合集3
随机推荐
Pytorch 训练技巧
动态规划 --- 状态压缩DP 详细解释
php如何去除字符串最后一位字符
tiup clean
Visual Studio 集成Qt开发环境的一些注意事项
武汉星起航:海外仓基础建设成为跨境电商企业的一大出海利器
nodejs environment variable settings
【C语言】指针和数组的深入理解(第二期)
tiup list
[TypeScript]简介、开发环境搭建、基本类型
Public Key Retrieval is not allowed报错解决方案
【HMS core】【FAQ】A collection of typical questions about Account, IAP, Location Kit and HarmonyOS 1
FME实现CAD带属性转SHP数据方法
Leetcode 118. 杨辉三角
在 Chrome 浏览器中安装 JSON 显示插件
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
Array element inverse
Promise笔记(一)
PMP每日一练 | 考试不迷路-7.30(包含敏捷+多选)
Core Topics under Microservice Architecture (2): Design Principles and Core Topics of Microservice Architecture