用相同的名称两次定义相同的Spring bean [英] Defining the same Spring bean twice with same name

查看:710
本文介绍了用相同的名称两次定义相同的Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring IOC中,对于一个具有相同名称和类的bean有两个定义吗?

Is having two definition for a bean (with same name and class) valid in Spring IOC ?

我在web.xml中包含两个bean定义文件.请参见下面的示例.

I am having two bean definition files included in web.xml. See the sample below.

applicationContext-beans1.xml

<bean name="myWao"
    class="com.beans.myBean">       
</bean> 

applicationContext-beans2.xml

<bean name="myWao"
    class="com.beans.myBean">       
</bean> 

到目前为止,我还没有遇到任何问题.但是,这可能会在多线程和集群的实际环境中产生影响吗?

I am not facing any issue till now. But, will this possibly impact in the real environment which will be multi threaded and clustered ?

注意:两种XML均已加载,因为我能够使用两种XML中定义的其他bean(仅一次)

Note: Both the XMLs are loaded as I am able to use the other beans defined(only once) in both the XMLs

推荐答案

它是有效的,但是您会发现一个bean被另一个bean覆盖.您会在日志中看到此为

It's valid, but you'll find that one bean is overridden by the other. You'll see this in the logs as

Overriding bean definition for...

此行为使您可以覆盖以前提供的bean定义.它会影响应用程序的静态组装,并且与问题中建议的线程/群集无关.

This behaviour allows you to override previously supplied bean definitions. It affects the static assembly of your app, and doesn't relate to threading/clustering as suggested in your question.

请注意,DefaultListableBeanFactory允许您通过 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆