2011-10-03 73 views
0

我需要在代碼執行期間「即時」創建對象的原型範圍實例。使用參數動態創建Prototype對象的實例

我知道的查找方法是很好的解決方案:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-lookup-method-injection

<!-- a stateful bean deployed as a prototype (non-singleton) --> 
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype"> 
<!-- inject dependencies here as required --> 
</bean> 

<!-- commandProcessor uses statefulCommandHelper --> 
<bean id="commandManager" class="fiona.apple.CommandManager"> 
<lookup-method name="createCommand" bean="command"/> 
</bean> 

我可以設置參數createCommand方法?

回答

2

我不認爲你可以,因爲容器不知道如何處理該參數。你期望它如何處理它?查找方法只獲取對象的新實例,從容器中獲取它。所以它在返回之前注入了所有的依賴關係。

如果您想傳遞額外參數 - 您可以在獲取實例後執行此操作。例如:

Foo someParam = ...; 
CommandManager manager = createCommand(); 
manager.doSomething(someParam); 
+0

感謝Bozho,我想過了,但我只是想確定... –