对于一个简单的spring引导应用程序,当时,我将面临一个意外的行为。
我在application.yml中定义了条件标志: service.enable=true
然后我创建了ServiceMesh.java接口,它应该由serviceA和serviceB实现,如下所示:
public interface ServiceMesh {
}
public class ServiceA implements ServiceMesh{
// ... code
}
public class ServiceB implements ServiceMesh{
// ... code
}我还定义了一个配置类:
@Configuration
public class ConditionOnPropertyConfiguration {
@Bean
@ConditionalOnProperty(prefix = "service", name = "enabled", havingValue = "true")
public ServiceMesh from(){
return new ServiceA();
}
@Bean
@ConditionalOnProperty(prefix = "service", name = "enabled", havingValue = "false")
public ServiceMesh from(Environment env){
return new ServiceB();
}
}在运行我的主课时:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}我得到了这个错误:没有“com.example.demo.service.ServiceMesh”类型的合格bean可用:预期至少有一个bean可以作为自动测试候选。依赖性注释:{}
预期行为是启动应用程序。
发布于 2020-12-14 21:57:15
将方法名称from更改为serviceA和serviceB。我已经将属性定义从service.enable=true更改为service.enabled=true。
@Bean
@ConditionalOnProperty(prefix = "service", name = "enabled", havingValue = "true")
public ServiceMesh serviceA() {
return new ServiceA();
}
@Bean
@ConditionalOnProperty(prefix = "service", name = "enabled", havingValue = "false")
public ServiceMesh serviceB() {
return new ServiceB();
}https://stackoverflow.com/questions/65296813
复制相似问题