[英]@PropertySource java.io.FileNotFoundException
I am testing Spring's @Conditional
in which I load the bean depending on the value present in the .properties
file. 我正在测试Spring的@Conditional
在其中根据.properties
文件中存在的值加载bean。 So I created a .properties
file in src/main/resources/application-config.properties
and I have the configuration class as: 所以我在src/main/resources/application-config.properties
创建了一个.properties
文件,我的配置类为:
@Configuration
@PropertySource(value = {"classpath:application-config.properties"}, ignoreResourceNotFound = false)
@ComponentScan(basePackages = {"com.app.test"})
public class ApplicationContextConfig {...}
I have 2 Condition
implementations as below: 我有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");
}
}
I have respective POJO classes as: 我有各自的POJO类:
@Component
@Conditional(value = ConditionalBeanOne.class)
public class BeanOne implements ServiceBean {}
@Component
@Conditional(value = ConditionalBeanTwo.class)
public class BeanTwo implements ServiceBean {}
When I run the application, I get following exception: Caused by: java.io.FileNotFoundException: class path resource [application-config.properties] cannot be opened because it does not exist
运行应用程序时,出现以下异常: Caused by: java.io.FileNotFoundException: class path resource [application-config.properties] cannot be opened because it does not exist
I am running this through main
method as following: 我正在通过以下main
方法运行此方法:
public class ConditionalMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationContextConfig.class);
.....
}
}
I couldn't reproduce your problem so I created a complete working example based on your use case, which is also available on my GitHub . 我无法重现您的问题,因此我根据您的用例创建了一个完整的工作示例,该示例也可以在我的GitHub上找到 。 I noticed that your conditions are really the same, only the values are different, so you don't really need to duplicate the code there. 我注意到您的条件实际上是相同的,只是值不同,因此您实际上不需要在此处重复代码。 Other than that, it's pretty much what you did. 除此之外,这几乎就是您所做的。
I'd say that you're reinventing the wheel here. 我想说的是您在这里重新发明轮子。 Spring Boot already has a ConditionalOnProperty which does this. Spring Boot已经有一个执行此操作的ConditionalOnProperty 。
Application.java : 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 : 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 : ConditionalOnProperty.java :
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
String value() default "";
}
OnPropertyCondition.java : 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 : application.properties :
greeting=hello
Run normally: 正常运行:
Output : 输出 :
Greeting: hello. 问候:你好。
Run with -Dgreeting=hi
: 使用-Dgreeting=hi
运行:
Output : 输出 :
Greeting: hi. 问候:嗨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.