In my Springboot application, I use an "utility" class (class having only static methods) in order to validate business rules on my configuration.
E.g.
@Component
public class MyClass {
@Autowired
public MyClass(MyConfig config) {
MyConfigValidator.validate(config);
}
...
If MyConfigValidator.validate()
finds that something is inconsistent in the configuration, it has to exit the Springboot application
According to my knowledge, the best/only way to exit properly a Springboot applications is to run:
SpringApplication.exit(context, () -> returnCode);
context
being an ApplicationContext
instance that must be injected.
My problem comes with the static
method MyConfigValidator.validate()
: being static it can't access to injected values
Let's have a look at MyConfigValidator
:
public class MyConfigValidator {
public static void validate(MyConfig config) {
if (!isValid(config)) {
doExit();
}
}
/// no need to detail here the isValid() method
private static void doExit() {
/// here, I don't know how to get the applicationContext
SpringApplication.exit(applicationContext, () -> returnCode);
}
}
Do you know how could I get the ApplicationContext
in my MyConfigValidator
class.
Thank you for your help
Regards,
Philippe
question from:
https://stackoverflow.com/questions/65830034/exit-springboot-from-static-function 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…