背景

在项目中使用Spring的Bean,一般都使用默认的Bean的单例,并且结合@Autowire使用。

实在有同一个类型多个实例的情况,也使用@Qualifier@Resource实现注入。

所以,对@Autowire的注入规则并不是特别的清楚。

今天突然想起这个疑惑,就用简单的实验确认一下。

实验的基础类

AppleBean,里面有个字符串的属性,和覆盖toString方法,用于在打印日志里更明显地分辨不同的bean:

public class AppleBean {

    private String content;

    public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} @Override
public String toString() {
return "AppleBean{" +
"content='" + content + '\'' +
'}';
} }

当容器中该类型只有一个Bean实例时,按类型注入,与Bean名字无关

注册一个Bean,叫appleBean:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class BeanConfig { @Bean
public AppleBean appleBean() {
AppleBean appleBean = new AppleBean();
appleBean.setContent("1");
return appleBean;
} }

将AppleBean注入到一个Controller中,并在此Controller的方法中打印AppleBean:

import com.example.demo.bean.AppleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HeartBeatController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private AppleBean appleBean; @GetMapping("/test")
public String test() {
this.logger.info("appleBean -> {}", appleBean);
return "SUCCESS";
} }

运行程序,并访问此测试接口,可见日志,appleBean成功注入

com.example.demo.HeartBeatController     : appleBean -> AppleBean{content='1'}

修改注入的属性名为appleBean2,与注册的bean名字appleBean是不一致的哦

import com.example.demo.bean.AppleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HeartBeatController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private AppleBean appleBean2; @GetMapping("/test")
public String test() {
this.logger.info("appleBean -> {}", appleBean2);
return "SUCCESS";
} }

再运行,日志如下,appleBean也正常注入进来了:

com.example.demo.HeartBeatController     : appleBean -> AppleBean{content='1'}

此节结论:

当容器中该类型只有一个Bean实例时,按类型注入,与Bean名字无关

当容器中该类型只有多个Bean实例时,按类型注入,与Bean名字有关

我们实例化两个AppleBean,名字分别为appleBean、appleBean2:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class BeanConfig { @Bean
public AppleBean appleBean() {
AppleBean appleBean = new AppleBean();
appleBean.setContent("1");
return appleBean;
} @Bean
public AppleBean appleBean2() {
AppleBean appleBean = new AppleBean();
appleBean.setContent("2");
return appleBean;
} }

我们注入appleBean2试试:

import com.example.demo.bean.AppleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HeartBeatController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private AppleBean appleBean2; @GetMapping("/test")
public String test() {
this.logger.info("appleBean -> {}", appleBean2);
return "SUCCESS";
} }

运行,可见日志,与名字精准匹配的Bean被注入进来

com.example.demo.HeartBeatController     : appleBean -> AppleBean{content='2'}

我们注入appleBean3试试,appleBean3是一个不存在的Bean名字:

import com.example.demo.bean.AppleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HeartBeatController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private AppleBean appleBean3; @GetMapping("/test")
public String test() {
this.logger.info("appleBean -> {}", appleBean3);
return "SUCCESS";
} }

日志如下,启动时就报异常了:

Field appleBean3 in com.example.demo.HeartBeatController required a single bean, but 2 were found:
- appleBean: defined by method 'appleBean' in class path resource [com/example/demo/bean/BeanConfig.class]
- appleBean2: defined by method 'appleBean2' in class path resource [com/example/demo/bean/BeanConfig.class]

我们再实例化另一种类(BoyBean,不是刚才的AppleBean哦),但将它注册的Bean名叫appleBean3:

@Bean
public BoyBean appleBean3() {
BoyBean boyBean = new BoyBean();
return boyBean;
}

再运行,仍然报错,与上面的异常是一致的:

Field appleBean3 in com.example.demo.HeartBeatController required a single bean, but 2 were found:
- appleBean: defined by method 'appleBean' in class path resource [com/example/demo/bean/BeanConfig.class]
- appleBean2: defined by method 'appleBean2' in class path resource [com/example/demo/bean/BeanConfig.class]

此节结论:

当容器中该类型只有多个Bean实例时,按类型注入,与Bean名字有关

【Spring】Spring的@Autowire注入Bean的规则测试的更多相关文章

  1. Spring的几种注入bean的方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式).   使用属性的se ...

  2. Spring IOC容器中注入bean

    一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...

  3. Spring的DI(Ioc) - 注入bean 和 基本数据类型

    注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...

  4. Spring中如何动态注入Bean实例教程

    前言 在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式,容易出现未被正确注入成功的情况. 本文将介绍一种在实际项目中基于动态的方式来提取Spring管理的Be ...

  5. Spring在Thread中注入Bean无效的解决方式

    在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务.而通过Spring注入来管理和使用服务是较为合理的方式.但是若直接在Thread子类中通过注解方式注入Bean是无 ...

  6. 在servlet中用spring @Autowire注入Bean

    在servlet中新增init方法: public void init(ServletConfig config) { super.init(config); SpringBeanAutowiring ...

  7. Spring框架知识总结-注入Bean的各类异常

    近日整合sping和hibernate框架时遇到了一系列的异常,本次主要说明一下spring框架可能出现的异常及解决方案. 我们借助sping强大的bean容器管理机制,通过BeanFactory轻松 ...

  8. 解决Spring+Quartz无法自动注入bean问题

    问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...

  9. Spring 注解Autowired自动注入bean异常解决

      错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...

  10. Spring为IOC容器注入Bean的五种方式

    一 @Import导入组件,id默认是组件的全类名 //类中组件统一设置.满足当前条件,这个类中配置的所有bean注册才能生效: @Conditional({WindowsCondition.clas ...

随机推荐

  1. 如何实现LLM的通用function-calling能力?

    众所周知,LLM的函数function-calling能力很强悍,解决了大模型与实际业务系统的交互问题.其本质就是函数调用. 从openai官网摘图: 简而言之: LLM起到决策的作用,告知业务系统应 ...

  2. 数据万象推出智能检索MetaInsight,现已开启限时公测

    海量文件的分析统计一直是对象存储COS的热点需求,伴随AIGC飞速迭代发展,在众多不同模态素材的海洋中,用户也急需更高效地管理和利用多媒体内容,打破传统搜索的桎梏. 数据万象推出的智能检索 MetaI ...

  3. ng-alain: delon/abc/sc 简化容器

    简化表单HTML模板的高阶组件,并进一步优化了一些细节: 更友好的表单校验状态 自动化响应式布局 自动维护表单 id 它由 se-container 容器(指令)和 se 组件来表示一个表单,一个简单 ...

  4. 【Word】文献引用批量上标

    \[([0-9]{1,2})\]

  5. Java代码覆盖率super-jacoco

    开源项目地址 https://gitee.com/didiopensource/super-jacoco 项目流程 项目架构 部署步骤 注意:一定要用Linux服务器部署,不要用Windows 准备L ...

  6. Nginx日志重定向到标准输出

    背景静态站点使用`docker`部署时,希望`nginx前台启动`的同时可以将错误日志和访问日志全部重定向到标准输出,便于采集和处理! 实现只需要修改`nginx.conf`中`3行`关于日志的配置就 ...

  7. 开源for Huawei,Beam适配GaussDB实践案例分享

    沃土云创开源开发者专项计划是华为给开源开发者提供专属激励资源,鼓励开发者积极参与开源 for Huawei适配,践行"让优秀开发者支持更优秀开发者"的理念. 之前我们介绍了fake ...

  8. Qt编写物联网管理平台41-自动清理早期数据

    一.前言 随着时间的增加,存储的历史记录也在不断增加,如果设备数量很多,存储间隔很短,不用多久,数据库中的记录就非常多,至少是百万级别起步,而且有些用户还是需要存储每一次的采集的数据,这数据量别说一年 ...

  9. [转]java调用python脚本以及通过Process.waitFor()直接调用python模块返回错误代码1的一种解决办法

    常见的java调用python脚本方式 通过jython提供的类库实现 通过Runtime.getRuntime()开启进程来执行脚本文件 通过jython提供的类库实现 通过jython实现的话,我 ...

  10. 得物自研客服IM中收发聊天消息背后的技术逻辑和思考实现

    本文由得物技术WWQ分享,原题"客服发送一条消息背后的技术和思",本文有修订和改动. 1.引言 在企业IM客服场景中,客服发送一条消息的背后,需要考虑网络通信.前端展示.后端存储以 ...