@Autowired 和 @Resource区别,简单测试容器中多个相同bean的情况

本文主要是介绍@Autowired 和 @Resource区别,简单测试容器中多个相同bean的情况,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

@Autowired 和 @Resource 区别

  • @Autowired 来自Spring, @Resource 来自java;
  • @Autowired 默认按类型注入,容器中存在多个相同类型的 Bean,将抛出异常。 可以配合使用 @Qualifier 指定名称。

两个相同类型(都 implements Formatter)的 Bean:

@Component("fooFormatter")
public class FooFormatter implements Formatter {public String format() {return "foo";}
}
@Component("barFormatter")
public class BarFormatter implements Formatter {public String format() {return "bar";}
}

直接使用 @Autowired:
容器启动失败: Field formatter in com.example.SpringInitialzrDemo.autowired.FooService required a single bean, but 2 were found

public class FooService {@Autowiredprivate Formatter formatter; // 容器启动失败: Field formatter in com.example.SpringInitialzrDemo.autowired.FooService required a single bean, but 2 were found
}

使用 @Autowired + @Qualifie:
容器启动成功

    @Autowired@Qualifier("barFormatter")private Formatter formatter;
  • @Resource 可以根据名称,可以根据类型。
    针对上面两个相同类型的 Bean,就可以使用
 	@Resource(name = "barFormatter") private Formatter formatter;

或:

 	@Resource(type = FooFormatter.class)private Formatter formatter;

@Autowired 和 @Resource 的参数

  • @Autowired 只有一个参数 required

@Autowired 里的参数 required = true (默认) 的用途:
启动容器时会校验这个Bean在容器中是否存在,不存在会阻止项目启动,并报错:

FooService required a bean of type 'xxx' that could not be found

如果使用 @Autowired(required = false) ,则不影响项目的启动。 当然,后续使用到还是会空指针。

  • @Resource 参数有多个,主要就使用name 和 type,分别指定名称和类型。

向容器注入多个相同名称的bean

使用@Component的方式:
public interface BeanService {
}
@Component("beanService")
public class OneServiceImpl implements BeanService{
}
@Component("beanService")
public class TwoServiceImpl implements BeanService{
}

启动时报错: BeanDefinitionStoreException:

Annotation-specified bean name 'beanService' for bean class [com.example.bean.TwoServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [com.example.bean.OneServiceImpl]

上诉bean有相同name且有相同类型(都是BeanService类型), 如果时不相同的类型呢:

@Component("beanService")
public class OneServiceImpl {
}
@Component("beanService")
public class TwoServiceImpl {
}

启动时还是报错:BeanDefinitionStoreException。

总结:使用@Component就是不能注入同名bean的,会在容器启动时就报错,且不受类型影响。
使用 @Bean方式

取消@Component注解,并使用一个Appfig类来注入:

@Configuration
public class AppConfig {@Bean("beanService")public BeanService oneServiceImpl() {return new OneServiceImpl();}@Bean("beanService")public BeanService twoServiceImpl() {return new TwoServiceImpl();}@Bean("beanService")public BeanService threeServiceImpl() {return new ThreeServiceImpl();}
}

@Bean注入三个bean,且名称都叫beanService。

启动容器没有报错。

  • 在测试类中,使用 @Autowired注入BeanService
    @Autowiredprivate BeanService beanService;

并输出bean实际类型:

    Class<? extends BeanService> aClass = beanService.getClass();System.out.println(aClass.getName());  // com.example.SpringInitialzrDemo.bean.OneServiceImpl

输出了OneServiceImpl,那么容器中到底有几个bean呢?

使用 applicationContext.getBeanDefinitionNames() 查看,只有一个 “beanService”。

  • 在测试类中,使用 @Resource注入BeanService,并强制指定类型为 TwoServiceImpl.class
    @Resource(type = TwoServiceImpl.class)private BeanService beanService;

结果报错:

BeanNotOfRequiredTypeException: Bean named 'beanService' is expected to be of type 'com.example.SpringInitialzrDemo.bean.TwoServiceImpl' but was actually of type 'com.example.SpringInitialzrDemo.bean.OneServiceImpl'

容器里是没有TwoServiceImpl, 是因为没有注入,还是被覆盖了?

继续测试,增加输出:

@Configuration
public class AppConfig {@Bean("beanService")public BeanService oneServiceImpl() {System.out.println("我要注入:OneServiceImpl");return new OneServiceImpl();}@Bean("beanService")public BeanService twoServiceImpl() {System.out.println("我要注入:TwoServiceImpl");return new TwoServiceImpl();}@Bean("beanService")public BeanService threeServiceImpl() {System.out.println("我要注入:ThreeServiceImpl");return new ThreeServiceImpl();}
}

结果:只输出了 “我要注入:OneServiceImpl”。

总结:使用 @Configuration + @Bean 的方式,注入多个同名bean,容器正常启动,但实际只有第一个被成功注入。

即便使用 @Primary 指定第二个优先。实际上还是注入的OneServiceImpl。(说明@Primary不是这么用的)

    @Bean("beanService")@Primarypublic BeanService twoServiceImpl() {System.out.println("我要注入:TwoServiceImpl");return new TwoServiceImpl();}

@Import的方式就暂不做测试了

上述例子是以注入多个相同名称不同类型的bean,接下来 测试注入多个相同类型的bean。

向容器注入多个相同类型的bean

@Configuration
public class AppConfig {@Beanpublic BeanService oneServiceImpl() {System.out.println("我要注入:OneServiceImpl");return new BeanService();}@Beanpublic BeanService twoServiceImpl() {System.out.println("我要注入:TwoServiceImpl");return new BeanService();}@Beanpublic BeanService threeServiceImpl() {System.out.println("我要注入:ThreeServiceImpl");return new BeanService();}
}
  • 使用@Autowired
    @Autowiredprivate BeanService beanService;

容器无法启动:

Field beanService in ___ required a single bean, but 3 were found
  • 使用 @Resource
    @Resourceprivate BeanService beanService;

容器无法启动:

BeanCreationException:No qualifying bean of type 'com.example.SpringInitialzrDemo.bean2.BeanService' available: expected single matching bean but found 3
  • 使用 @Autowired + @Qualifier 并指定bean名称
    @Autowired@Qualifier("oneServiceImpl")private BeanService beanService;

容器可以正常启动。

  • 使用 @Resource(name = “twoServiceImpl”)
    @Resource(name = "twoServiceImpl")private BeanService beanService;

容器可以正常启动。

  • 使用@Primary
@Configuration
public class AppConfig {@Beanpublic BeanService oneServiceImpl() {System.out.println("我要注入:OneServiceImpl");return new BeanService();}@Bean@Primarypublic BeanService twoServiceImpl() {System.out.println("我要注入:TwoServiceImpl");return new BeanService();}@Beanpublic BeanService threeServiceImpl() {System.out.println("我要注入:ThreeServiceImpl");return new BeanService();}
}

这样,使用 @Autowired 或@Resource 时不指定名称,就会默认使用@Primary的这个bean,容器也可以正常启动。

    @Resourceprivate BeanService beanService;
总结:使用 @Configuration + @Bean 相同类型但是不同名称bean时,这些同类型的bean都能被创建,但必须在注入时指定bean名称,或使用@Primary标识其中一个bean的优先级。

验证,输出所有的bean:

	ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringInitialzrDemoApplication.class, args);String[] names = applicationContext.getBeanDefinitionNames();for (String name : names) {System.out.println(">>>>>>" + name);}

结果:会找到三个bean确实都是成功创建.

>>>>>>oneServiceImpl
>>>>>>twoServiceImpl
>>>>>>threeServiceImpl

这篇关于@Autowired 和 @Resource区别,简单测试容器中多个相同bean的情况的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1046546

相关文章

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

Go之errors.New和fmt.Errorf 的区别小结

《Go之errors.New和fmt.Errorf的区别小结》本文主要介绍了Go之errors.New和fmt.Errorf的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考... 目录error的基本用法1. 获取错误信息2. 在条件判断中使用基本区别1.函数签名2.使用场景详细对

Redis中哨兵机制和集群的区别及说明

《Redis中哨兵机制和集群的区别及说明》Redis哨兵通过主从复制实现高可用,适用于中小规模数据;集群采用分布式分片,支持动态扩展,适合大规模数据,哨兵管理简单但扩展性弱,集群性能更强但架构复杂,根... 目录一、架构设计与节点角色1. 哨兵机制(Sentinel)2. 集群(Cluster)二、数据分片

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想

一文带你迅速搞懂路由器/交换机/光猫三者概念区别

《一文带你迅速搞懂路由器/交换机/光猫三者概念区别》讨论网络设备时,常提及路由器、交换机及光猫等词汇,日常生活、工作中,这些设备至关重要,居家上网、企业内部沟通乃至互联网冲浪皆无法脱离其影响力,本文将... 当谈论网络设备时,我们常常会听到路由器、交换机和光猫这几个名词。它们是构建现代网络基础设施的关键组成

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知