I am trying to implement Spring Condition org.springframework.context.annotation.Condition
as follows:
public class APIScanningDecisionMaker implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// Not able to read the property "swagger.scanner.can.run". It is always NULL.
String canRunFlagInStr = context.getEnvironment().getProperty("swagger.scanner.can.run");
// Ignore the rest of the code.
}
}
However, as shown in the above code, when I read the property "swagger.scanner.can.run" it is always NULL. In my property file I have set it to "swagger.scanner.can.run=false".
I also tried to use the @Value("${swagger.scanner.can.run}")
but even that returns NULL. When I put a debug in this method I can see that it is being called.
Just for the sake of completion I am using the APIScanningDecisionMaker
as follows:
@Configuration
@EnableSwagger
@Conditional(APIScanningDecisionMaker.class)
public class CustomSwaggerConfig {
// Ignore the rest of the code
}
Is there any reason why "swagger.scanner.can.run" is being retrieved as NULL?
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,
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
.
You can manually load the properties file, if due to any problem its not loading into spring's context at the time it is constructing your Condition.
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
try {
Properties appProps = new Properties();
appProps.load(context.getResourceLoader().getResource("classpath:app.properties").getInputStream());
String canRunFlagInStr = appProps.getProperty("swagger.scanner.can.run");
return Boolean.parseBoolean(canRunFlagInStr);
} catch (IOException e) {
throw new ApplicationContextException(("Exception while loading context", e);
}
}
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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.