2013-07-02 64 views
0

一種簡單的新手問題...我看我如何創建一個字符串對象,但我將如何創建一個int對象?如何創建一個在IOC上下文文件中定義的Int32類型的spring .Net獨立對象?

下面是從我的上下文文件中的XML代碼片斷:

<object id="myString" type="System.String"> 
    <constructor-arg value="foo" />  
</object> 
<object id="myInt" type="System.Int32"> 
    <<<**** how do I set this ****>>>> 
</object> 
+0

如果你想要這個能夠注入一些配置值,那麼你應該考慮使用['PropertyPlaceHolderConfigurer'](http://www.springframework.net/doc-latest/reference/html/objects.html #對象 - 工廠placeholderconfigurer)。 – Marijn

回答

3

嘗試這種情況:

<object id="MyInt" type="System.Int32" factory-method="Copy"> 
    <constructor-arg index="0"> 
    <value>123</value> 
    </constructor-arg> 
</object> 
0

嘗試這種情況:

<object id="MyInt" type="System.Int32" factory-method="Parse"> 
    <constructor-arg index="0"> 
    <value>123</value> 
    </constructor-arg> 
</object> 

爲了創建原始類型系統的目的.Int32,您必須使用factory-method =「Parse」。屬性factory-method =「Copy」不起作用,因爲它不存在於System.Int32類型中,並且您必須使用靜態方法來執行此操作。

相關問題