Spring Java Configuration - how do create a map of enums to beans-references
You probably want something like this:
@Configuration
public class MyConfiguration {
@Bean public Map<ColourEnum, ColourHandler> colourHandlers() {
Map<ColourEnum, ColourHandler> map = new EnumMap<>();
map.put(WHITE, whiteHandler());
// etc
return map;
}
@Bean public ColourHandler whiteHandler() {
return new WhiteHandler();
}
}
If you need to keep your handlers as @Component
s, then you can autowire them into the configuration class:
@Configuration
public class MyConfiguration {
@Autowired private WhiteColourHandler whiteColourHandler;
@Bean public Map<ColourEnum, ColourHandler> colourHandlers() {
Map<ColourEnum, ColourHandler> map = new EnumMap<>();
map.put(WHITE, whiteColourHandler);
return map;
}
}
Similar to the accepted answer except that, instead of autowiring components, you can declare the beans in the configuration class as usual and pass them as arguments to the Map bean method:
@Configuration
public class MyConfiguration {
@Bean public Map<ColourEnum, ColourHandler> colourHandlers(ColourHandler whiteHandler) {
Map<ColourEnum, ColourHandler> map = new EnumMap<>();
map.put(WHITE, whiteHandler);
return map;
}
@Bean public ColourHandler whiteHandler() {
return new WhiteHandler();
}
}
Also note that the injection of the map as a @Resource doesn't need the annotation's "name" parameter if the field name follows the same naming convention as the bean definition.
i.e. This would work without the name parameter:
@Resource
private Map<ColourHandlerEnum, ColourHandler> colourHandlers;
but this would require it:
@Resource(name="colourHandlers")
private Map<ColourHandlerEnum, ColourHandler> handlers;
This is actually pretty simple but you need to know how:
@Autowired private ColourHandler whiteColourHandler;
...
public Map<ColourEnum, ColourHandler> getColourHander() {
Map<ColourEnum, ColourHandler> result = ...;
map.put( ColourEnum.white, whiteColourHandler );
...
return map;
}
The trick is that you can inject beans into a config.
Since you already have a unique class/@Component for each ColorHandler, I would just let Spring figure out what to use (no need for @Autowire injection nor any additional creation methods):
@Configuration
public class MyConfiguration {
@Bean public Map<ColourEnum, ColourHandler> colourHandlers(
WhiteColourHandler whiteHandler,
BlueColourHandler blueHandler,
RedColourHandler redHandler) {
Map<ColourEnum, ColourHandler> map = new EnumMap<>();
map.put(WHITE, whiteHandler);
map.put(BLUE, blueHandler);
map.put(RED, redHandler);
return map;
}
}