@Autowired默认注入问题?

ioc容器存在“同一类型”通过@Bean生成的对象和@Component注解扫描的对象,使用@Autowired注解导入ioc容器中该类型对象,问题是得到的对象是通过那个... ioc容器存在“同一类型”通过@Bean生成的对象和@Component注解扫描的对象,使用@Autowired注解导入ioc容器中该类型对象,问题是得到的对象是通过那个注解生成的?跟两个注解的优先级相关吗?还是其他什么原因? 展开
 我来答
澈丶233
2020-08-11 · TA获得超过169个赞
知道小有建树答主
回答量:446
采纳率:94%
帮助的人:106万
展开全部

看了楼上两位的说法,你所谓的“同一类型”感觉我还是没太理解。我结合楼上两位的说法做了测试:

以@Component和@Bean注册同一个类A:


定义组件A:

定义Bean A,这里我先把Bean注释掉,等会说为啥:

在测试类中注入两次A:

运行结果:

然后令人惊讶的事实是,我把Bean的注释放开:

此时运行:

震惊,两次的运行结果都输出了name为bean。

综上,@Component和@Bean同时注册一个类时,感觉@Bean所谓的“优先级”更高。

大雅新科技有限公司
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,... 点击进入详情页
本回答由大雅新科技有限公司提供
软件开发领路人
2020-08-11 · 解答软件开发新人的问题烦恼
软件开发领路人
采纳数:711 获赞数:408

向TA提问 私信TA
展开全部
你好,很高兴回答你的问题。如果Spring管理了两个同类型的对象。通过@Autowired来注入的话会报错。因为Spring不知道要选哪个。
可以在要选的那个对象注解处同时加注解@Primary。这样@Autowired处就会注入用@Primary注解的对象。
如果有帮助到你,请点击采纳。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
mimiba212
2020-08-11 · TA获得超过413个赞
知道小有建树答主
回答量:1504
采纳率:64%
帮助的人:85.2万
展开全部
没有优先级说法
@Component
This is a generic annotation and can be applied to any class of the application to make it a spring managed component(simply, generic stereotype for any spring managed component). when the classpath is scanned by the spring’s component-scan (@ComponentScan) feature, it will identify the classes annotated with @Component annotation (within the given package) and create the beans of such classes and register them in the ApplicationContext. @Component is a class level annotation and its purpose it to make the class as spring managed component and auto detectable bean for classpath scanning feature.
if you want to know more about @Component and other stereo type annotations, it is recommended to look at this article.

@Bean
@Bean is used to explicitly declare and register a bean (as a configuration bean) in Spring IOC container that is returned from a method. @Bean is a method level annotation and it is used within a class that is annotated with @Configuration. Simply, @Bean annotation is used to register the bean returned by a method as a spring configuration bean in IOC Container. @Bean is only a method level annotation and it cannot be used with classes and object declaration.
@Bean annotation indicates that a method produces a bean that should be managed by the Spring container.
To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a ApplicationContext. By default, the bean name will be the same as the method name.The following is a simple example of a @Bean method declaration.

@Configurationpublic class ApplicationConfig { @Bean
public User adminUserProfile()
{
return new User("Chathuranga","Tennakoon");
}
}

In the ApplicationConfig class, you can see that we first use the @Configuration annotation to inform Spring that this is a Java-based configuration file. Afterward, the @Bean annotation is used to declare a Spring bean and the DI requirements.
The @Bean annotation is equivalent to the <bean> tag, the method name is equivalent to the id attribute within the <bean> tag.
I hope that after reading this article, you have got a clear idea about the real purpose and use of @Bean and @Component annotations.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
荷塘月色1111
2020-08-13 · 超过51用户采纳过TA的回答
知道小有建树答主
回答量:181
采纳率:89%
帮助的人:32.6万
展开全部
搞不懂这个问题有何意义?同一类型还要分别用两个不同的注解来注入容器?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式