Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

java - Exit Springboot from static function

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of trying to shutdown the application manually, I would create an ApplicationRunner that validates the state on startup of the application. The application will fail to start if an exception is thrown. For instance:

@Component
public class SomeInitializer implements ApplicationRunner {

    private MyConfigValidator configValidator;

    // If it's a Spring bean, inject it
    public SomeInitializer(MyConfigValidator configValidator) {
        this.configValidator = configValidator;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (!configValidator.isValid()) {
            throw new IllegalStateException("Some description");
        }
    }
}

Furthermore, if you want to perform validation on configuration properties on startup, you might want to consider using a @Validated @ConfigurationProperties bean instead:

@Validated
@ConfigurationProperties(prefix="some.prefix")
public class YourProperties {
    @NotEmpty
    private String someProperty;

    // getter and setter
}

More info on using validated ConfigurationProperties can be found here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...