NumberFormat JavaDoc says:
Number formats are generally not synchronized. It is recommended to create separate format instances for each thread.
I want to obtain an instance to use in my objects. One instance per thread, or one instance per Object (objects are not shared among threads). At the moment the object has an instance variable containing NumberFormat.
private NumberFormat nFormat = NumberFormat.getInstance(Locale.ITALY);
This is already fine, I shouldn’t have any race condition, since Objects are not shared.
However, I don’t know how to use Spring to configure this, I know about factory-method, but I don’t know how to pass a Locale to it.
<bean factory-method="getInstance" class="java.text.NumberFormat"
scope="prototype" />
How to declare my NumberFormat bean properly?
This is how you could do it:
However, I probably wouldn’t use Spring at all to do it, I’d use a static factory method instead.