@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中Switch Case多个条件处理方法举例

《Java中SwitchCase多个条件处理方法举例》Java中switch语句用于根据变量值执行不同代码块,适用于多个条件的处理,:本文主要介绍Java中SwitchCase多个条件处理的相... 目录前言基本语法处理多个条件示例1:合并相同代码的多个case示例2:通过字符串合并多个case进阶用法使用

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

go 指针接收者和值接收者的区别小结

《go指针接收者和值接收者的区别小结》在Go语言中,值接收者和指针接收者是方法定义中的两种接收者类型,本文主要介绍了go指针接收者和值接收者的区别小结,文中通过示例代码介绍的非常详细,需要的朋友们下... 目录go 指针接收者和值接收者的区别易错点辨析go 指针接收者和值接收者的区别指针接收者和值接收者的

售价599元起! 华为路由器X1/Pro发布 配置与区别一览

《售价599元起!华为路由器X1/Pro发布配置与区别一览》华为路由器X1/Pro发布,有朋友留言问华为路由X1和X1Pro怎么选择,关于这个问题,本期图文将对这二款路由器做了期参数对比,大家看... 华为路由 X1 系列已经正式发布并开启预售,将在 4 月 25 日 10:08 正式开售,两款产品分别为华

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@