Spring @Conditional:错误消息

时间:2016-08-04 10:13:07

标签: java spring error-handling

@Configuration
public class MyConfig {

    @Bean
    public Foobar foobar(@Value("${my.value}") String v) {
        return new Foobar(v);
    }

}

@Component
public class MyComp {

    private final Foobar foobar;

    public MyComp(Foobar foobar) {
        this.foobar = foobar;
        System.out.println(foobar);
    }

}

如果未设置 my.value ,我将收到此错误消息

java.lang.IllegalArgumentException: Could not resolve placeholder 'my.value' in string value "${my.value}"

现在 my.value 也受@ConditionalOnProperty(“my.value”)保护

@Configuration
public class MyConfig {

    @ConditionalOnProperty("my.value")
    @Bean
    public Foobar foobar(@Value("${my.value}") String v) {
        return new Foobar(v);
    }

}

如果未设置 my.value ,我将收到此错误消息

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myComp' defined in file [/home/stephan/workspaces/workspace_mercateo/demo/target/classes/com/example/MyComp.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [com.example.Foobar] found for dependency [com.example.Foobar]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.Foobar] found for dependency [com.example.Foobar]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
[...]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.Foobar] found for dependency [com.example.Foobar]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

虽然spring认识到 my.value 缺失,但输出日志中没有任何内容。

有没有办法配置弹簧这样 1)使用@Conditional 和 2)错误消息包含“ my.value 缺失” ?

0 个答案:

没有答案