0

I have the below Spring configuration in an XML file:

<util:map id="myService">
    <entry key="s1" value-ref="goodService" />
    <entry key="s2" value-ref="betterService" />
</util:map>

is there a way to migrate this to Annotation based configuration, i.e.

@Bean
public Map<String, MyService> serviceMap() {
    Map<String, MyService> map = new HashMap<>();
    ...

So the values in the Map are references to beans.

hamid
  • 2,033
  • 4
  • 22
  • 42

1 Answers1

1

In the config class autowire the instances and place the properties to the map

@Autowired
private GoodService goodService;
@Autowired
private BetterService betterService;

@Bean
public Map<String, MyService> serviceMap() {
    Map<String, MyService> map = new HashMap<>();
    map.put("s1", goodService);
    map.put("s2", betterService);
StanislavL
  • 56,971
  • 9
  • 68
  • 98