【Spring】Spring的@Autowire注入Bean的规则测试
背景
在项目中使用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的规则测试的更多相关文章
- Spring的几种注入bean的方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的se ...
- Spring IOC容器中注入bean
一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...
- Spring的DI(Ioc) - 注入bean 和 基本数据类型
注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...
- Spring中如何动态注入Bean实例教程
前言 在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式,容易出现未被正确注入成功的情况. 本文将介绍一种在实际项目中基于动态的方式来提取Spring管理的Be ...
- Spring在Thread中注入Bean无效的解决方式
在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务.而通过Spring注入来管理和使用服务是较为合理的方式.但是若直接在Thread子类中通过注解方式注入Bean是无 ...
- 在servlet中用spring @Autowire注入Bean
在servlet中新增init方法: public void init(ServletConfig config) { super.init(config); SpringBeanAutowiring ...
- Spring框架知识总结-注入Bean的各类异常
近日整合sping和hibernate框架时遇到了一系列的异常,本次主要说明一下spring框架可能出现的异常及解决方案. 我们借助sping强大的bean容器管理机制,通过BeanFactory轻松 ...
- 解决Spring+Quartz无法自动注入bean问题
问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...
- Spring 注解Autowired自动注入bean异常解决
错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...
- Spring为IOC容器注入Bean的五种方式
一 @Import导入组件,id默认是组件的全类名 //类中组件统一设置.满足当前条件,这个类中配置的所有bean注册才能生效: @Conditional({WindowsCondition.clas ...
随机推荐
- manim边做边学--文字的创建与销毁
本篇开始介绍Manim中的动画模块,动画模块是整个框架的核心魅力所在. Manim不仅提供了可以直接实现各种各样动画效果的对象, 还提供了设置动画的时长.延迟时间以及运动速率等参数,可以据此发挥自己的 ...
- windows server系统中,Pro运行深度学习工具错误
安装深度学习包后,运行相关工具的时候报错,缺失cv2的模块. 在arcpy执行窗口,直接去引入cv2包的时候,确实发了错误. 查看了相关路径,确认cv2的包,在对应路径已经存在,也有对应的元数据信息, ...
- C# 获取系统盘符
1.使用.net管理对象(引入System.Management) public static List<string> getDisk() { WqlObjectQuery wmique ...
- vue开发一个简单的组件
首先在项目中新建一个js文件 在文件内创建一个对象,对象内创建install方法,将对象用export default暴漏出去 export default{ install(){ console.l ...
- FastExcel 合并单元格(相当的行数据,进行合并)
目录 需求 思路 实现 Excel导出单元格全量合并策略 日期格式转换 接口代码 Service DTO 使用FastExcel数据导出:官网: https://idev.cn/fastexcel/z ...
- 中电金信:技术实践|Flink多线程实现异构集群的动态负载均衡
导语:Apache Flink是一个框架和分布式处理引擎,用于对无界和有界数据流进行有状态计算.本文主要从实际案例入手并结合作者的实践经验,向各位读者分享当应用场景中异构集群无法做到负载均衡时,如何通 ...
- Netty 中ChannelOption的含义以及使用的场景Netty 中ChannelOption的含义以及使用的场景
一.概述 最近在写一个分布式服务框架,打算用netty框架做底层网络通信,关于netty的学习可以参考如下资料: http://blog.csdn.net/column/details/enjoyne ...
- [转]CLion 2019去掉灰色参数提示(parameters hints)
众所周知,clion是一个很好用的c plus plus IDE,刚装好的clion默认的设置多少有一些不符合口味的地方,在查看代码或者敲代码的时候看到如下这样的灰色提示,我是有点受不了的: 之前用的 ...
- 在命令中输入信息创建maven项目
参考链接: 1.使用命令行创建maven web项目 2.Maven 三种archetype说明 3.maven创建项目时在generating project in interactive mode ...
- 墨卡托及Web墨卡托投影解析
Google Maps.Virtual Earth等网络地理所使用的地图投影,常被称作Web Mercator(Web墨卡托投影)或Spherical Mercator(球面墨卡托投影),它与常规墨卡 ...