简体   繁体   English

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

[英]Best practices for validating the configuration of a Spring web application

I would like that my Spring-based web application were able to validate its configuration during startup. 我希望我的基于Spring的Web应用程序能够在启动期间验证其配置。

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 注意:我的方法是使用一个特殊的EnvironmentValidation bean来检查配置/文件夹结构是否正常,如果没有,它会抛出异常

During startup all errors will be logged in to server logs. 在启动期间,所有错误都将记录到服务器日志中。 Logs can be checked and system administrator can be notified. 可以检查日志并通知系统管理员。

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. 如果您需要通知,则可以设置电子邮件通知程序,例如使用log4j,它将在启动时向系统管理员发送任何异常。

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. 检查文件夹存在并且是可读/可写的,这不是我在webapp中真正做的事情 - 这属于您的部署基础架构。 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. 假设您可以将自己的自定义检查编写为spring bean,首先加载这些检查并在某些配置验证失败时抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM