问题
I would like that my Spring-based web application were able to validate its configuration during startup.
This means for example:
- check if the required folders exist and are readable/writable
- check if the required configuration keys are set and consistent
- ...
- check any other constraint that is required for the correct functioning
How can you perform these checks and notify the system administrator if something is wrong?
The goal is to reduce the risk that some critical error arises when the application is actually going to need those resources that are bound to the wrong configuration.
NOTE: my approach is to use a special EnvironmentValidation bean that checks if the configuration/folder structure is ok and if not it throws an exception
回答1:
If you want notifications you could set up an email notifier for example using log4j that would send any exceptions on startup to the system administrator.
if a configuration key is not present spring will not start up anyway.
check the keys are consistent with what?
checking folders exist and are readable/writable isn't something I'd really do in my webapp - this belongs in your deployment infrastructure. Saying that you could write your own custom checks as spring beans, load these first and throw an exception if some of your configuration fails validation.
回答2:
During startup all errors will be logged in to server logs. Logs can be checked and system administrator can be notified.
来源:https://stackoverflow.com/questions/8832790/best-practices-for-validating-the-configuration-of-a-spring-web-application