2013-03-05 53 views
4

我是Spring的世界的新手。在一次採訪中,有人問我們是否可以在沒有指定class(即,該bean只有id屬性)的情況下用XML創建一個bean。我沒有答案。請指教,如果我們可以在Spring中創建一個XML而不指定class屬性,並且我們通常會在這種情況下執行此操作。在沒有class屬性的xml中定義bean

+0

如果您使用的是工廠,那麼你可以做到這一點,但你仍然必須指定工廠bean和方法 – 2013-03-05 10:07:28

+2

@ArunPJohny請提供一個小例子,這將是有益的完全掌握。預先感謝 – user2129402 2013-03-05 10:10:31

+0

參考文檔http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-class-static-factory-方法 – 2013-03-05 10:44:42

回答

3

春天documentation使得它相當清楚:

<!-- 
    Each bean definition must specify the fully qualified name of the class, 
    except if it pure serves as parent for child bean definitions. 
--> 
<!ATTLIST bean class CDATA #IMPLIED> 

This section解釋得很詳細,並提供有用的例子。

+1

Bilinski可以請你詳細解釋下哪一節我們只指定bean id,而不是bean繼承部分的類名稱 – user2129402 2013-03-05 10:17:20

+0

爲了方便,我附上了一個鏈接到Spring文檔。你會發現一個簡單的例子(搜索「inheritedTestBeanWithoutClass」)。 – 2013-03-05 10:27:24

+0

<豆ID = 「inheritedTestBeanWithoutClass」 抽象= 「真」> <屬性名= 「名稱」 的值= 「父」/> <屬性名= 「年齡」 值= 「1」/> <豆id =「inheritsWithClass」class =「org.springframework.beans.DerivedTestBean」 parent =「inheritedTestBeanWithoutClass」init-method =「initialize」> <! - age將從父bean定義中繼承1的值 - > – user2129402 2013-03-05 10:51:12

相關問題