spring 在单个应用程序上下文中定义相同类的两个 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19324508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
define two beans of same class in single application context
提问by ssm
if I am defining two beans of same class and not giving any scope. Then how many instance of class will get created. for example
如果我定义了同一个类的两个 bean 而没有给出任何范围。然后将创建多少类实例。例如
in applicationContext.xml
在 applicationContext.xml 中
<bean name="testBean" class="com.test.Example"/>
<bean name="myBean" class="com.test.Example"/>
采纳答案by Arturo Volpe
Spring will create two beans of the type com.test.Exampleand the autowiring will be for the type or method name (or Qualifiers), see Spring IOC
Spring 将创建该类型的两个 bean,com.test.Example自动装配将用于类型或方法名称(或限定符),请参阅Spring IOC
See this simple test:
看这个简单的测试:
With this class
有了这个班
public static class TestBean {
static int INT = 1;
public int test;
public TestBean() {
test = INT++;
}
}
Configuration xml:
配置xml:
<bean name="testBean" class="com.test.TestBean"/>
<bean name="myBean" class="com.test.TestBean"/>
JUnit4 with spring container test:
JUnit4 与 spring 容器测试:
@Resource
TestBean testBean;
@Resource
TestBean myBean;
@Test
public void test() {
assertNotNull(testBean);
assertNotNull(myBean);
assertFalse(testBean == myBean);
assertFalse(testBean.test == myBean.test);
}
This test dont fail, as you see, two beans of type TestBean are created.
该测试不会失败,如您所见,创建了两个 TestBean 类型的 bean。
See this part in the Spring Doc:
在 Spring Doc 中查看此部分:
byName
Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property.byType
Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set.constructor
Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
byName
按属性名称自动装配。Spring 查找与需要自动装配的属性同名的 bean。例如,如果一个 bean 定义被设置为按名称自动装配,并且它包含一个 master 属性(即它有一个 setMaster(..) 方法),Spring 会查找一个名为 master 的 bean 定义,并使用它来设置财产。byType
如果容器中只存在一个属性类型的 bean,则允许自动装配属性。如果存在多个,则会引发致命异常,这表明您不能为该 bean 使用 byType 自动装配。如果没有匹配的 bean,则什么也不会发生;该属性未设置。构造函数
类似于 byType,但适用于构造函数参数。如果容器中没有一个构造函数参数类型的 bean,则会引发致命错误。
回答by Hariprasad
Yes. Spring will create two instances in this scenario. Spring container creates a singleton instance per bean definition. EX:
是的。在这种情况下,Spring 将创建两个实例。Spring 容器为每个 bean 定义创建一个单例实例。前任:
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("ws.xml");
TestBean teatBean = (TestBean) ac.getBean(TestBean.class);
TestBean myBean1 = (TestBean) ac.getBean(TestBean.class);
System.out.println("a : " + teatBean.test + " : "
+ teatBean.getName());
teatBean.setName("a TEST BEAN 1");
System.out.println("uPdate : " + teatBean.test + " : "
+ teatBean.getName());
System.out.println("a1 : " + myBean1.test + " : " + myBean1.getName());
myBean1.setName(" a1 TEST BEAN 10");
System.out.println("a1 update : " + teatBean.test + " : "
+ myBean1.getName());
}
}
<bean class="com.spring4hibernate4.TestBean">
<constructor-arg name="i" value="1"></constructor-arg>
<property name="name" value="1-name"></property>
</bean>
<bean class="com.spring4hibernate4.TestBean">
<constructor-arg name="i" value="10"></constructor-arg>
<property name="name" value="10-name"></property>
</bean>
</beans>
public class Test {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("ws.xml");
TestBean teatBean = (TestBean) ac.getBean(TestBean.class);
TestBean myBean1 = (TestBean) ac.getBean(TestBean.class);
System.out.println("a : " + teatBean.test + " : "
+ teatBean.getName());
teatBean.setName("a TEST BEAN 1");
System.out.println("uPdate : " + teatBean.test + " : "
+ teatBean.getName());
System.out.println("a1 : " + myBean1.test + " : " + myBean1.getName());
myBean1.setName(" a1 TEST BEAN 10");
System.out.println("a1 update : " + teatBean.test + " : "
+ myBean1.getName());
}
}
回答by Hitesh
Spring will create two instances in this scenario. Spring container creates a singleton instance per bean definition.
在这种情况下,Spring 将创建两个实例。Spring 容器为每个 bean 定义创建一个单例实例。
When you call getContext.getBean("testBean") will always give you same instance for testBean bean definition
当您调用 getContext.getBean("testBean") 时,将始终为 testBean bean 定义提供相同的实例
When you call getContext.getBean("myBean") will always give you same instance for myBean bean definition.
当您调用 getContext.getBean("myBean") 时,将始终为 myBean bean 定义提供相同的实例。

