[英]NumberFormat thread safety with Spring
NumberFormat JavaDoc說:
數字格式通常不同步。 建議為每個線程創建單獨的格式實例。
我想獲得一個要在我的對象中使用的實例。 每個線程一個實例,或者每個對象一個實例(線程之間不共享對象)。 目前,該對象具有一個包含NumberFormat的實例變量。
private NumberFormat nFormat = NumberFormat.getInstance(Locale.ITALY);
這已經很好,我不應該有任何競爭條件,因為不共享對象。
但是,我不知道如何使用Spring來配置它,我知道有關factory-method
,但是我不知道如何將Locale傳遞給它。
<bean factory-method="getInstance" class="java.text.NumberFormat"
scope="prototype" />
如何正確聲明我的NumberFormat bean?
這是您可以執行的操作:
<bean id="numberFormat" factory-method="getInstance"
class="java.text.NumberFormat" scope="prototype">
<constructor-arg>
<util:constant static-field="java.util.Locale.ITALY" />
</constructor-arg>
</bean>
但是,我可能根本不會使用Spring來做到這一點,而應該使用靜態工廠方法。
我認為您可以使用:
<bean factory-method="getInstance" class="java.text.NumberFormat"
scope="prototype">
<constructor-arg><ref bean="locale_bean"/></constructor-arg>
</bean>
constructor-arg元素用於傳遞構造對象所需的bean和值。 另請參閱依賴項注入示例以獲取更多信息:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.