Java @ConditionalOnProperty Bean配置未按预期工作

Java @ConditionalOnProperty Bean配置未按预期工作,java,spring,spring-boot,conditional-statements,Java,Spring,Spring Boot,Conditional Statements,对于一个简单的SpringBoot应用程序,当我尝试有条件地恢复beans时,我将面临一个意外的行为 我已经在application.yml中定义了条件标志:service.enable=true 然后我创建了ServiceMesh.java接口,应该由serviceA和serviceB实现,如下所示: public interface ServiceMesh { } public class ServiceA implements ServiceMesh{ // ... code } pu

对于一个简单的SpringBoot应用程序,当我尝试有条件地恢复beans时,我将面临一个意外的行为

我已经在application.yml中定义了条件标志:service.enable=true

然后我创建了ServiceMesh.java接口,应该由serviceA和serviceB实现,如下所示:

public interface ServiceMesh {
}

public class ServiceA implements ServiceMesh{
// ... code
}

public class ServiceB implements ServiceMesh{
// ... code
}
我还定义了一个配置类:

运行我的主类时:

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
我得到了这个错误: 没有“com.example.demo.service.ServiceMesh”类型的合格bean可用:至少需要1个符合autowire候选条件的bean。依赖项批注:{}


预期的行为是启动应用程序

将方法名
更改为
serviceA
serviceB
。我已将属性定义从
service.enable=true
更改为
service.enabled=true

@Bean
@ConditionalOnProperty(prefix=“service”,name=“enabled”,havingValue=“true”)
公共服务网格服务A(){
返回新的ServiceA();
}
@豆子
@ConditionalOnProperty(prefix=“service”,name=“enabled”,havingValue=“false”)
公共服务网格服务B(){
返回新的ServiceB();
}

是的,这似乎是真正问题的一个旁路,如果您查看ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsForBeanMethod,您将看到如果config类已经跳过了同名的方法,它将跳过bean定义;这种行为在任何地方都没有记录。
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}