16

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

in applicationContext.xml

<bean name="testBean" class="com.test.Example"/>
<bean name="myBean" class="com.test.Example"/>
4

3 回答 3

12

Spring will create two beans of the type com.test.Example and the autowiring will be for the type or method name (or Qualifiers), see 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:

<bean name="testBean" class="com.test.TestBean"/>
<bean name="myBean" class="com.test.TestBean"/>

JUnit4 with spring container test:

@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.

See this part in the 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.

于 2013-10-11T18:17:17.930 回答
0

Spring will create two instances in this scenario. Spring container creates a singleton instance per bean definition.

When you call getContext.getBean("testBean") will always give you same instance for testBean bean definition

When you call getContext.getBean("myBean") will always give you same instance for myBean bean definition.

于 2014-11-05T13:00:37.733 回答
0

Yes. Spring will create two instances in this scenario. Spring container creates a singleton instance per bean definition. EX:

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());

    }

}
于 2015-09-15T15:35:23.150 回答