且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

验证Spring Web应用程序的配置的***实践

更新时间:2023-10-13 09:45:52

如果你想要通知,你可以设置一个电子邮件通知器,例如使用log4j,在启动时向系统管理员发送任何异常。



如果配置键不存在,则春天将无法启动。



检查键是否与什么一致?



检查文件夹是否存在并且可读/写不是我真正想在我的webapp中做的 - 这属于在部署基础架构中。说你可以将自己的自定义检查写为spring bean,如果你的配置没有通过验证,就先加载这些检查并抛出异常。


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

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.