且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

处理一个 Spring bean/接口的多个实现

更新时间:2021-11-02 02:59:24

广告.1:你可以使用 @Qualifier 注释 或使用 @Resource 自动装配,而不是 @Autowired,后者默认为字段名称而不是类型.

Ad. 1: you can use @Qualifier annotation or autowire using @Resource as opposed to @Autowired which defaults to field name rather than type.

广告.2:它会在运行时失败,说两个 bean 正在实现这个接口.如果你的一个 bean 是另外 @Primary 注释,按类型自动装配时优先使用.

Ad. 2: It will fail at runtime saying that two beans are implementing this interface. If one of your beans is additionally annotated with @Primary, it will be preferred when autowiring by type.