验证Spring Web应用程序的配置的最佳实践 [英] Best practices for validating the configuration of a Spring web application

查看:201
本文介绍了验证Spring Web应用程序的配置的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的基于Spring的网络应用程序能够在启动期间验证其配置。



这意味着例如: / p>


  • 检查所需文件夹是否存在且可读写

  • / strong>



如何执行这些检查并通知系统管理员如果出现错误?



目标是减少当应用程序实际需要绑定到错误配置的资源时出现一些关键错误的风险

注意:我的方法是使用一个特殊的 EnvironmentValidation bean,检查配置/文件夹结构是否确定,如果不是它抛出异常

解决方案

如果你想要通知,你可以设置一个电子邮件通知器,例如使用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.

这篇关于验证Spring Web应用程序的配置的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆