繁体   English   中英

@PropertySource java.io.FileNotFoundException

[英]@PropertySource java.io.FileNotFoundException

我正在测试Spring的@Conditional在其中根据.properties文件中存在的值加载bean。 所以我在src/main/resources/application-config.properties创建了一个.properties文件,我的配置类为:

@Configuration
@PropertySource(value = {"classpath:application-config.properties"}, ignoreResourceNotFound = false)
@ComponentScan(basePackages = {"com.app.test"})
public class ApplicationContextConfig {...}

我有2个Condition实现,如下所示:

public class ConditionalBeanOne implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        String name= conditionContext.getEnvironment().getProperty("condition.name");
        return name.equalsIgnoreCase("condition_one");
    }
}


public class ConditionalBeanTwo implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        String name= conditionContext.getEnvironment().getProperty("condition.name");
        return name.equalsIgnoreCase("condition_two");
    }
}

我有各自的POJO类:

@Component
@Conditional(value = ConditionalBeanOne.class)
public class BeanOne implements ServiceBean {}

@Component
@Conditional(value = ConditionalBeanTwo.class)
public class BeanTwo implements ServiceBean {}

运行应用程序时,出现以下异常: Caused by: java.io.FileNotFoundException: class path resource [application-config.properties] cannot be opened because it does not exist

我正在通过以下main方法运行此方法:

public class ConditionalMain {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
        .....
    }
}

我无法重现您的问题,因此我根据您的用例创建了一个完整的工作示例,该示例也可以在我的GitHub上找到 我注意到您的条件实际上是相同的,只是值不同,因此您实际上不需要在此处重复代码。 除此之外,这几乎就是您所做的。

我想说的是您在这里重新发明轮子。 Spring Boot已经有一个执行此操作的ConditionalOnProperty

Application.java

public class Application {
    public static void main(String[] args) {
        try (AnnotationConfigApplicationContext applicationContext =
                     new AnnotationConfigApplicationContext(ApplicationConfig.class)) {

            ApplicationConfig.GreeterService greeterService =
                    applicationContext.getBean(ApplicationConfig.GreeterService.class);

            String actual = greeterService.greeting();

            System.out.printf("Greeting: %s.\n", actual);
        }
    }
}

ApplicationConfig.java

@Configuration
// The / doesn't matter, but I prefer being explicit
@PropertySource("classpath:/application.properties")
@ComponentScan
public class ApplicationConfig {

    @FunctionalInterface
    public static interface GreeterService {
        String greeting();
    }

    @Bean
    @ConditionalOnProperty("hello")
    public GreeterService helloService() {
        return () -> "hello";
    }

    @Bean
    @ConditionalOnProperty("hi")
    public GreeterService hiService() {
        return () -> "hi";
    }
}

ConditionalOnProperty.java

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
    String value() default "";
}

OnPropertyCondition.java

public class OnPropertyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Map<String, Object> attributes = annotatedTypeMetadata
                .getAnnotationAttributes(ConditionalOnProperty.class.getName());
        String value = (String) attributes.get("value");

        String name = conditionContext.getEnvironment().getProperty("greeting");

        return !isEmpty(name) && name.equalsIgnoreCase(value);
    }
}

application.properties

greeting=hello

正常运行:

输出

问候:你好。

使用-Dgreeting=hi运行:

输出

问候:嗨。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM