Spring Java 配置 - 如何创建枚举到 bean 引用的映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13293297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:10:09  来源:igfitidea点击:

Spring Java Configuration - how do create a map of enums to beans-references

javaspring

提问by Mannie

with Java based configuration, i am trying to convert a map that maps enums to bean references to be in pure java config (currently in XML & works) but can't seem to find anything in the documentations;

使用基于 Java 的配置,我正在尝试将映射枚举到 bean 引用的映射转换为纯 Java 配置(目前在 XML 和作品中),但似乎无法在文档中找到任何内容;

Currently, my XML like so;

目前,我的 XML 是这样的;

<util:map id="colourHanders" key-type="com.example.ColourEnum"
          value-type="com.example.ColourHandler">
    <entry key="white" value-ref="whiteColourHandler"/>
    <entry key="blue" value-ref="blueColourHandler"/>
    <entry key="red" value-ref="redColourHandler"/>
</util:map>

I'm sure it is easy but again, can't find anything on the subject of how to represent this in Pure Java (so I don't have any XML configuration files)..

我确信这很容易,但同样,找不到关于如何在纯 Java 中表示这一主题的任何内容(所以我没有任何 XML 配置文件)。

Note; the ColourHandlerbeans are created using the @Component annotation, e.g..

笔记; 在ColourHandler豆使用@Component注释,例如创建。

@Component
public class RedColourHandler implements ColourHander{
.....
}

and the map of colourHandlers is referenced as so;

并且 colorHandlers 的映射是这样引用的;

@Resource(name="colourHandlers")
    private Map<ColourHandlerEnum, ColourHandler> colourHandlers;

Thanks,

谢谢,

Ian.

伊恩。

采纳答案by hertzsprung

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 @Components, then you can autowire them into the configuration class:

如果您需要将处理程序保留为@Components,那么您可以将它们自动装配到配置类中:

@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;
    }
}

回答by inor

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):

由于您已经为每个 ColorHandler 拥有了一个唯一的类/@Component,我只会让 Spring 找出要使用的内容(不需要 @Autowire 注入或任何其他创建方法):

@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;
    }
}

回答by Aaron Digulla

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.

诀窍是您可以将 bean 注入到配置中。

回答by SimonSparks

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:

与接受的答案类似,不同之处在于,您可以像往常一样在配置类中声明 bean,并将它们作为参数传递给 Map bean 方法,而不是自动装配组件:

@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.

另请注意,如果字段名称遵循与 bean 定义相同的命名约定,则将映射作为 @Resource 注入不需要注释的“名称”参数。

i.e. This would work without the name parameter:

即这将在没有 name 参数的情况下工作:

@Resource
private Map<ColourHandlerEnum, ColourHandler> colourHandlers;

but this would require it:

但这需要它:

@Resource(name="colourHandlers")
private Map<ColourHandlerEnum, ColourHandler> handlers;