我有2个实现的接口。我需要在配置文件中显式设置要使用的实现。目前,我收到一个错误“原因:启动Spring Boot应用程序时,元素[api.services [0] .esrigeolocationfinderserviceimpl,api.services [1] .peliasgeolocationfinderserviceimpl]未被绑定” 。这些是类:
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 }
您知道如何在接口列表中设置值吗?
谢谢
答案 0 :(得分:0)
您可以通过在@Component
类@PropertySource("classpath:api.services[0]")