Spring environment validation -


we're building spring-based application delivered end users distribution package. users responsible configuring whatever needs configured (it's various filesystem locations, folder access permissions, etc). there's idea make app users understand not configured or parts of configuration invalid.

our current approach custom applicationcontextinitializer environment validation "manually" , registers few "low level" beans in application context explicitly. if wrong, initializer throws, exception caught somewhere in main(), interpreted (converted plain english) , displayed.

while approach works fine, i'm wondering if there best practices minimize hand-written code , use spring whenever possible.

here's illustrative example. application requires folder file uploads. means:

  1. there should configuration file
  2. this file should accessible app
  3. this file should have no syntax errors
  4. this file should explicitly define specific property (let app.uploads.folder)
  5. this property should describe existing filesystem entity
  6. this entity should folder
  7. the app should have read/write access folder

does spring provide tools implement sort of validation easily?

spring boot has nice feature context , external configuration validation. if define pojo class , declare @configurationproperties spring bind environment (external properties , system/os typically) properties using databinder. e.g.

@configurationproperties(name="app.uploads") public class fileuploadproperties {     private file folder;     // getters , setters ommitted } 

will bind app.uploads.folder , ensure file. validation can manually in setter, or can implement validator in fileuploadproperties or can use jsr-303 annotations on fields. default external property in app.uploads.* doesn't bind throw exception (e.g. mis-spelled property name, or conversion/format error).

if use spring boot autoconfigure @enableautoconfigure don't have else, if it's vanilla spring (boot) need @enableconfigurationproperties in @configuration somewhere well.

a bonus feature: if use spring boot actuator jmx , http support (in webapp) inspecting bindable , bound properties of @configurationproperties beans. http endpoint "/configprops".


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -