【问题标题】:How do I define a specific bean with a qualifier that would designate a specific bean for its dependancies?如何定义具有限定符的特定 bean,该限定符将为其依赖项指定特定 bean?
【发布时间】:2022-01-29 22:36:44
【问题描述】:

首先感谢您抽出时间提供帮助。你们都很棒!

我的问题: 假设我的 application.yml 中有一些嵌套属性

regions:
 region1:
  delayTime: 10
  threads: 2
 region2:
  delayTime: 5
  threads: 1

以及具有区域配置的相应组件

@Component
public class Region{
   public int delayTime;
   public int threads;
}
@Configuration
public class RegionConfig{
   @Bean("region1props")
   @ConfigurationProperties(prefix ="regions.region1")
   public Region getRegion1(){
        returns new Region();
    }

   @Bean("region2props")
   @ConfigurationProperties(prefix ="regions.region2")
   public Region getRegion2(){
        returns new Region();
    }

}

如果说我还有一个 RegionHandler 组件,该组件具有 Region 作为依赖项:

@Component
public class RegionHandler{
 @AutoWired 
 Region region;
}

最终我想要一个没有配置的 RegionOnePublisherRegionTwoPublisher 组件:

@Component
public class RegionOnePublisher{
  @Autowired
   RegionHandler regionOne;// <-- need to configure Region depenandcy as "region1props" bean 
}
//----------------------------------------------
@Component
public class RegionTwoPublisher{
  @Autowired
   RegionHandler regionTwo; // <-- need to configure Region depenandcy as "region2props" bean
} 

我将如何定义 RegionHandler 对象的配置类,以便 @Bean("region1props") 是为 RegionHandler regionOne 注入的 Region bean 依赖关系,@Bean("region2props") 作为 Region 注入 RegionHandler regionTwo 的 bean 依赖关系?

我所拥有的:[已编辑]

@Component
public class RegionOnePublisher{
  @Autowired
  @Qualifier("regionOneProps")
   RegionHandler regionOne;// <-- need to configure Region depenandcy as "region1props" bean 
}
//----------------------------------------------
@Component
public class RegionTwoPublisher{
  @Autowired
  @Qualifier("regionTwoProps")
   RegionHandler regionTwo; // <-- need to configure Region depenandcy as "region2props" bean
} 
}

谢谢各位!

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    如果您在RegionHandler 上使用构造函数注入并且没有使用@Component 注释该类,它应该可以帮助您使用特定的Region 来创建bean。

    public class RegionHandler {
    
        Region region;
    
        public RegionHandler(Region region) {
            this.region = region;
        }
    }
    

    现在您的处理程序可以通过稍微更改配置文件来注册为 bean

    @Configuration
    public class RegionHandlerConfig{
    
        @Bean("regionHandler1")
        public RegionHandler getRegionHandlerOne(@Qualifier("region1props") Region region) {
            return new RegionHandler(region);
        }
    
        @Bean("regionHandler2")
        public RegionHandler getRegionHandlerTwo(@Qualifier("region2props") Region region) {
            return new RegionHandler(region);
        }
    }
    

    我没有编译和检查,但这是基本的想法。

    另外,一定要研究一下这种模式是否可以扩展。我猜您在每个区域的处理程序和发布者中都有特定的逻辑。但是我想知道如果您必须添加更多区域,您将如何扩展它。更好的结构可能是从配置中获取区域地图,并且每个只有一个处理程序和发布者 bean。

    【讨论】:

    • 感谢您的回复!我本来想这样做,但这是一个更大的项目的一部分,开发人员让我感到兴奋和干燥。大量的代码覆盖率,基本上他以 RegionHandler 无法配置但将特定 bean 注入 Publisher 的方式构建它。我刚刚编辑了最后一个代码块
    • 所以我从您的评论中了解到,您根本无法更新 RegionHandler 代码,但仍然需要为每个区域分开发布者。并且您想从 RegionPublisher 中决定 RegionHandler 的字段依赖关系。您可以使用 Reflection 从 RegionPublisher 修改 RegionHandler 的依赖关系,但我建议不要这样做。考虑到 RegionHandler 已经在 spring 上下文中,您所做的任何更改都将贯穿整个项目。如果你愿意,你可以在每个 RegionPublisher 中创建一个新的 RegionHandler 实例,然后使用 Reflection。
    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 2018-07-25
    • 2016-06-28
    • 2016-01-12
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多