gpt4 book ai didi

java - 是否可以在@Component类中的3个或更多bean之间进行优先级排序?

转载 作者:行者123 更新时间:2023-11-30 06:05:46 24 4
gpt4 key购买 nike

为了澄清起见,我有一个带有两个有线 bean 的 @Component 类。我想优先考虑每个 bean 。这可以吗?

@Component
public class SecureMessages {

@Autowired // load second
private ObjectMapper objectMapper;

@Autowired // load first
private LocalCacheServiceImpl LocalCacheServiceImpl;
}

根据上面的代码,我想在调用此组件类时首先创建一个用于本地缓存的 bean,然后可以创建其余的 bean。

最佳答案

您可以向组件添加@Order注释。订单值越低,优先级越高。您可以阅读更多相关信息here .

@Bean
@Order(2)
public ObjectMapper getObjectMapper(){
return new ObjectMapper();
}

@Bean
@Order(1)
public LocalCacheServiceImpl getLocalCacheServiceImpl(){
return new LocalCacheServiceImpl();
}

@DependsOn 可能也许也适合您。这取决于您想要订购咖啡 bean 的原因。您可以阅读here

关于java - 是否可以在@Component类中的3个或更多bean之间进行优先级排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com