Spring Core Annotations
创始人
2025-05-29 15:58:35
0

1. Overview

We can leverage the capabilities of Spring DI engine using the annotations in the org.springframework.beans.factory.annotation and org.springframework.context.annotation packages.

We often call these “Spring core annotations” and we’ll review them in this tutorial.


2. DI-Related Annotations

2.1. @Autowired

We can use the @Autowired to mark a dependency which Spring is going to resolve and inject. We can use this annotation with a constructor, setter, or field injection.

  • @Autowired has a boolean argument called required with a default value of true. It tunes Spring’s behavior when it doesn’t find a suitable bean to wire. When true, an exception is thrown, otherwise, nothing is wired.

  • Note, that if we use constructor injection, all constructor arguments are mandatory. Starting with version 4.3, we don’t need to annotate constructors with @Autowired explicitly unless we declare at least two constructors.


2.2. @Bean

@Bean marks a factory method which instantiates a Spring bean:

@Bean
Engine engine() {return new Engine();
}

Spring calls these methods when a new instance of the return type is required.

The resulting bean has the same name as the factory method. If we want to name it differently, we can do so with the name or the value arguments of this annotation (the argument value is an alias for the argument name):

@Bean("engine")
Engine getEngine() {return new Engine();
}

Note, that all methods annotated with @Bean must be in @Configuration classes.


2.3. @Qualifier

We use @Qualifier along with @Autowired to provide the bean id or bean name we want to use in ambiguous situations.

For example, the following two beans implement the same interface:

class Bike implements Vehicle {}class Car implements Vehicle {}

If Spring needs to inject a Vehicle bean, it ends up with multiple matching definitions. In such cases, we can provide a bean’s name explicitly using the @Qualifier annotation.

2.3.1. Using constructor injection:

@Autowired
Biker(@Qualifier("bike") Vehicle vehicle) {this.vehicle = vehicle;
}

2.3.2. Using setter injection:

@Autowired
void setVehicle(@Qualifier("bike") Vehicle vehicle) {this.vehicle = vehicle;
}

Alternatively:

@Autowired
@Qualifier("bike")
void setVehicle(Vehicle vehicle) {this.vehicle = vehicle;
}

2.3.3. Using field injection:

@Autowired
@Qualifier("bike")
Vehicle vehicle;

2.4. @Required

@Required on setter methods to mark dependencies that we want to populate through XML:

@Required
void setColor(String color) {this.color = color;
}


Otherwise, BeanInitializationException will be thrown.


2.5. @Value


参考:
Spring Core Annotations

相关内容

热门资讯

78.qt QCustomPl... 参考https://www.qcustomplot.com/index.php/tutorials/...
开发一个进销存系统大概的时间及... 不同的企业进销存管理千差万别、个性化程度高,市场上的产品无法覆盖各个行业的管理需求。 ...
网站流量飙升背后:外贸企业谷歌... 自从我涉足外贸行业,我逐渐认识到谷歌SEO优化在提升网站流量和吸引潜在客户方面的重要性...
根号杂题选做 qwq [Violet]蒲公英 传送门 分块,离散化后维护前 iii 个块中数j的出现...
Selenium+Pytest... 如果下方文字内容没有看明白的话,我推荐大家看一套视频,比文字内容讲的更加...
美国专利法中对于发生在专利公开... 摘要:一般情况下,侵权damage是从专利的授权日开始计算,...
C语言枚举—事件提醒程序 (t... 编写程序,实现一个事件提醒程序,如果今天是周几,完成什么事...
【实战】React 必会第三方... 文章目录一、引子二、配置使用1.安装2.使用(1)直接调用(...
Prompt交易平台;Chat... 👀日报&周刊合集 | 🎡生产力工具与行业应用大全 | ᾞ...
阿里巴巴2017实习生笔试题(... 具体题目来自阿里巴巴2017实习生笔试题,本文仅为整理与汇总。 本题应该往C+...
Redis(十四):性能问题 前言 上一篇介绍了 Redis 作为缓存服务器的问题。这节开始介绍 Redis 性能方面的问题。Re...
linux 全局环境变量删除后... linux 全局环境变量删除后 还有 仍然存在1、编辑 /etc/profile2、设置REDISC...
HD 钱包涉及的 BIP32、... 引言 随着比特币区块链的发展,人们已经不满足于,只有一个账号的情况&#x...
Python数据结构与算法(p... 主要内容:什么是列表查找顺序查找二分查找一、什么是查找?(...
JVM字符串常量池String... String的基本特性 String:字符串,使用一对""引起来表示。 ...
超详细-安装vCenterv ... 目录 介绍: 第一阶段安装: 第二阶段安装: 最近在玩虚拟...
2023年PMP考生|考前必练... “日日行,不怕千万里;常常做,不怕千万事。”每日五题&#x...
刷题(二) 目录标题题目列表第一题解析方法1:递归方法2:迭代第二题解析解法一&#x...
机器学习实战2--logsti... logisticlogisticlogistic回归实际上是一个二分类问题。 问题描述:...
删除mac启动台launchp... 第一种情况 在Mac上安装Photoshop CS6的后, 启动台(LaunchPad...