Can I have a factory as follows?
public class Factory
{
private static Map<EnumXyz, IDAO> map = new HashMap<Sting, Object>();
public static void init()
{
//how do i initialize my map through spring initialization
}
public static IDAO getDAO(EnumXyz dao)
{
if (map.containsKey(dao))
return map.get(dao);
else
{
throw new IllegalArgumentException("dao not supported " + dao);
}
return null;
}
}
- How do I take care of the initialization of my factory through spring?
- Is this way of building a factory correct?
- Any other, better approach?
I would instantiate your factory as a bean itself, and have an instance of it – don’t make everything static. Spring itself can control whether your bean is a singleton (it will default as such).
e.g.
and your Spring config:
In your constructor, pass in the map which is defined itself in your Spring config.