【发布时间】: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;
}
最终我想要一个没有配置的 RegionOnePublisher 和 RegionTwoPublisher 组件:
@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