在我的春天,Module.xml我有兩個豆子:Java組件實例錯誤豆
<bean id="two_num" class="main.java.com.shen.Generator">
<property name="length" value="8" />
<property name="categories" value="3" />
...
</bean>
<bean id="six_num" class="main.java.com.shen.Generator">
<property name="length" value="6" />
<property name="categories" value="1" />
<property name="numeric" value="numeric" />
...
</bean>
我實例化我的課像這樣:
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
Generator obj = (Generator) context.getBean("two_num");
由於種種原因,Java是始終實例我的第二個bean,儘管我明確表示我想要bean「two_num」。如果我要翻轉這個命令,並在「two_num」上面有「six_num」的bean,它會得到底部的bean。 :|這是怎麼回事?我很困惑。我的選擇特定bean的方法是否錯誤?
編輯:增加一個bean來它在這之後是我所得到的,當我運行我的程序:
信息:預實例中org.springframework.beans.factory.support單身.DefaultListableBeanFactory @ e949f69:定義bean [two_num,six_num,four_num];工廠層次的根
和預期的一樣,當我在我的課調用打印方法,我可以看到,當前對象實例化與four_num dicated的信息,而不是「two_num」
類Generator的外觀如何? – vcetinick
@vcetinick這個類很漂亮,但基本上它有一些私有變量,沒有構造函數。它有setLength,setCategories,setNumeric方法。 –
是任何這些變量靜態或最終? – vcetinick