@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

相关文章

Java逻辑运算符之&&、|| 与&、 |的区别及应用

《Java逻辑运算符之&&、||与&、|的区别及应用》:本文主要介绍Java逻辑运算符之&&、||与&、|的区别及应用的相关资料,分别是&&、||与&、|,并探讨了它们在不同应用场景中... 目录前言一、基本概念与运算符介绍二、短路与与非短路与:&& 与 & 的区别1. &&:短路与(AND)2. &:非短

C# 委托中 Invoke/BeginInvoke/EndInvoke和DynamicInvoke 方法的区别和联系

《C#委托中Invoke/BeginInvoke/EndInvoke和DynamicInvoke方法的区别和联系》在C#中,委托(Delegate)提供了多种调用方式,包括Invoke、Begi... 目录前言一、 Invoke方法1. 定义2. 特点3. 示例代码二、 BeginInvoke 和 EndI

Java中数组转换为列表的两种实现方式(超简单)

《Java中数组转换为列表的两种实现方式(超简单)》本文介绍了在Java中将数组转换为列表的两种常见方法使用Arrays.asList和Java8的StreamAPI,Arrays.asList方法简... 目录1. 使用Java Collections框架(Arrays.asList)1.1 示例代码1.

MySQL常见的存储引擎和区别说明

《MySQL常见的存储引擎和区别说明》MySQL支持多种存储引擎,如InnoDB、MyISAM、MEMORY、Archive、CSV和Blackhole,每种引擎有其特点和适用场景,选择存储引擎时需根... 目录mysql常见的存储引擎和区别说明1. InnoDB2. MyISAM3. MEMORY4. A

Java8需要知道的4个函数式接口简单教程

《Java8需要知道的4个函数式接口简单教程》:本文主要介绍Java8中引入的函数式接口,包括Consumer、Supplier、Predicate和Function,以及它们的用法和特点,文中... 目录什么是函数是接口?Consumer接口定义核心特点注意事项常见用法1.基本用法2.结合andThen链

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea

在Dockerfile中copy和add的区别及说明

《在Dockerfile中copy和add的区别及说明》COPY和ADD都是Dockerfile中用于文件复制的命令,但COPY仅用于本地文件或目录的复制,不支持自动解压缩;而ADD除了复制本地文件或... 目录在dockerfile中,copy 和 add有什么区别?COPY 命令ADD 命令总结在Doc

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

MySQL进阶之路索引失效的11种情况详析

《MySQL进阶之路索引失效的11种情况详析》:本文主要介绍MySQL查询优化中的11种常见情况,包括索引的使用和优化策略,通过这些策略,开发者可以显著提升查询性能,需要的朋友可以参考下... 目录前言图示1. 使用不等式操作符(!=, <, >)2. 使用 OR 连接多个条件3. 对索引字段进行计算操作4

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告: