Spring - How do you set Enum keys in a Map with annotations

后端 未结 7 559
有刺的猬
有刺的猬 2020-12-13 09:08

I\'ve an Enum class

public enum MyEnum{
    ABC;
}

than my \'Mick\' class has this property

private Map

        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 09:48

    This worked for me...

    My Spring application context:

    
      
      
    
    

    My class where the Map gets injected:

    public class MyClass {
    
        private @Resource Map myMap;
    }
    

    The important things to note are that in the Spring context I used SpEL (Spring Expression Language) which is only available since version 3.0. And in my class I used @Resource, neither @Inject (it didn't work for me) nor @Autowired (I didn't try this). The only difference I'm aware of between @Resource and @Autowired, is that the former auto-inject by bean name while the later does it by bean type.

    Enjoy!

提交回复
热议问题