Best practices for validating the configuration of a Spring web application

末鹿安然 提交于 2020-01-14 14:27:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!