我正在测试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 {...}我有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);
.....
}
}发布于 2017-07-08 23:07:50
我无法重现您的问题,因此我根据您的用例创建了一个完整的工作示例,它也可以在我的GitHub上使用。我注意到您的条件是完全相同的,只是值是不同的,所以您不需要在那里复制代码。除此之外,你所做的也差不多。
我得说你在重新发明方向盘。Spring已经有了一个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一起运行
输出
问候:嗨。
https://stackoverflow.com/questions/44990594
复制相似问题