我有一个有 2 个实现的接口(interface)。我需要在配置文件中明确设置我将使用的实现。目前,我在启动 Spring Boot 应用程序时收到错误“原因:元素 [api.services[0].esrigeolocationfinderserviceimpl,api.services[1].peliasgeolocationfinderserviceimpl] 未绑定(bind)”。以下是类(class):
public interface MyService {
// Some methods here
}
@Data
@Component
@NoArgsConstructor
public class MyFirstServiceImpl implements MyService {
private String first;
}
@Data
@Component
@NoArgsConstructor
public class MySecondServiceImpl implements MyService {
private String second;
}
@Data
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix="api")
public class MyConfig {
@Autowired
private List<MyService> services;
}
application.yml:
api:
services[0]: { first: aValue }
services[1]: { second: anotherValue }
你知道我们如何设置接口(interface)列表中的值吗?
谢谢
最佳答案
您可以通过在 @Component
类中使用注释来实现此目的 @PropertySource("classpath:api.services[0]")
关于java - 在 YML 中的接口(interface)列表中设置类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58199440/