Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject(autowired) beans to map with enum as map key in spring?

I've learned that in spring, i can autowire/inject into Map<String, SomeBeanInterface> by configured name like below:

public interface DummyInterface{
}

@Component("impl1")
public class Impl1 implement DummyInterface{
}

@Component("impl2")
public class Impl2 implement DummyInterface{
}

public class SomeUsage{
    @Autowired
    private Map<String, DummyInterface> mapping;
    // ...
}

and retrieve the Component by string as key like:

SomeUsage use = new SomeUsage();
DummyInterface dummy = use.getMapping().get("impl1");
// do sth...

However, if the key of bean mapping is not the type of String, but the type of user defined Enum, how should i inject the beans into the enumMap?

I've read some post and learned that it can be configured by xml file. But it seems to be that the xml configuration is tightly coupled with the <Enum, Bean> pair, which means that each time if i add a new <Enum, Bean> pair, i have to synchronize the configuration file, it seems that there's no difference comparing to my current solution, that is, still using the <String, Bean> collection and maintain the <Enum, String> mapping in java code by my own. Are there any better solution to handle this? Or do i miss something?

like image 536
user8510613 Avatar asked Sep 12 '19 02:09

user8510613


1 Answers

You always have to define mapping between Enum and Spring Bean but you can enforce that components have to declare to which enumeration they are mapped to. You can acheive that creating interface like:

public interface EnumMappedBean {
    SomeEnum getSomeEnum();
}

Then every component that you want to be mapped has to implement it.

@Component
public class Bean1 implements EnumMappedBean {
    @Override
    public SomeEnum getSomeEnum() {
        return SomeEnum.ENUM1;
    }
}

@Component
public class Bean2 implements EnumMappedBean {
    @Override
    public SomeEnum getSomeEnum() {
        return SomeEnum.ENUM2;
    }
}

Then you can map each of this components by it's enumaration.

@Configuration
public class AppConfig {
    @Bean
    public Map<SomeEnum, EnumMappedBean> getBeansMappedByEnum(@NonNull Collection<EnumMappedBean> enumBeans) {
        return enumBeans.stream()
                .collect(toMap(EnumMappedBean::getSomeEnum, Function.identity()));
    }
}

And inject wherever you want.

@Service
public class SomeOtherBean {

    private Map<SomeEnum, EnumMappedBean> beansMappedByEnum;

    @Autowired
    public SomeOtherBean(@NonNull Map<SomeEnum, EnumMappedBean> beansMappedByEnum) {
        this.beansMappedByEnum = beansMappedByEnum;
    }
}

In config class you can also validate that every component declare uniqe, non-null enum value.

like image 94
kemot90 Avatar answered Sep 28 '22 00:09

kemot90