gpt4 book ai didi

java - 覆盖 Spring bean

转载 作者:行者123 更新时间:2023-12-02 01:53:36 24 4
gpt4 key购买 nike

在我使用的库中定义了以下 bean。

package co.random.core;

@Service
public class ApiEndpoints {

@Value("${testApi}")
private String testApi;

public String getTestApi() {
return testApi;
}
}

然后将其注入(inject)库的另一部分,

@Autowired
ApiEndpoints apiEndpoints;

有没有办法覆盖这个bean?

我在代码中尝试了以下内容,

@Configuration
public class ApiConfiguration {

@Primary
@Bean(name = "apiEndpoints")
public CustomEndpoints getCustomApiEndpoints() {
return new CustomEndpoints();
}
}

但这会引发错误,

Field apiEndpoints in co.random.core.RandomService required a bean of type 'co.random.core.ApiEndpoints' that could not be found.

最佳答案

该 bean 已被覆盖,但您定义的新 bean 不符合预期 Autowiring 的类型。您的 bean 类型必须是(或扩展)ApiEndpoints

关于java - 覆盖 Spring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52617353/

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