I am trying to implement Spring Condition org.springframework.context.annotation.Condition
as follows:
public class APIScanningDecisionMaker impl
Maybe spring doesn't know the file ?
This can be fix on annotated your class where the @Value("${swagger.scanner.can.run}")
is used :
@PropertySource(value="classpath:config.properties")
Regards,
this works fine for me
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
try {
InputStream i = conditionContext.getResourceLoader().getResource("prop.file").getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(i));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
System.out.println(out.toString());
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
The object of class implementing "Condition" is created via constructor from Spring, so you can not inject values using @Value annotation. You can do something like below in your code:
@Override
public boolean matches(
ConditionContext arg0,
AnnotatedTypeMetadata arg1) {
String prop = conditionContext.getEnvironment()
.getProperty("arg0");
//further code
}
If you add @Conditional
to config class then APIScanningDecisionMaker
should implement ConfigurationCondition
. And don't forget to add @PropertySource
to config class.
import org.springframework.context.annotation.ConfigurationCondition;
public class APIScanningDecisionMaker implement ConfigurationCondition {
@Override
public ConfigurationPhase getConfigurationPhase() {
return ConfigurationPhase.REGISTER_BEAN;
}
}
Properties will have been loaded in ConfigurationPhase.REGISTER_BEAN
phase.
If you use @Conditional
for methods then you could implement Condition
.