我正在测试Spring的@Conditional
,其中我根据.properties
文件中的值加载bean。所以我在.properties
中创建了一个src/main/resources/application-config.properties
文件,我的配置类为:
@Configuration
@PropertySource(value = {"classpath:application-config.properties"}, ignoreResourceNotFound = false)
@ComponentScan(basePackages = {"com.app.test"})
public class ApplicationContextConfig {...}
我有两个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);
.....
}
}
答案 0 :(得分:1)
我无法重现您的问题所以我根据您的用例创建了一个完整的工作示例,该示例也可以在我的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
:
<强>输出强>:
问候:嗨。