gpt4 book ai didi

java - @PropertySource java.io.FileNotFoundException

转载 作者:行者123 更新时间:2023-12-02 02:43:46 24 4
gpt4 key购买 nike

我正在测试 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 {}

当我运行应用程序时,出现以下异常:原因:java.io.FileNotFoundException:类路径资源[application-config.properties]无法打开,因为它不存在

我通过 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

正常运行:

输出:

Greeting: hello.

使用-Dgreeting=hi运行:

输出:

Greeting: hi.

关于java - @PropertySource java.io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44990594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com