我有 bean AddressBean,它具有一些属性,如 addressLine1、city 等。在使用 AddressBeanService 将其持久化到 DB 之前,我正在对其进行预验证,并在保存后,使用 ValidateAddressService 中的 preValidate 和 postValidate 函数对其进行验证。而这一切都是我从 AddressBeanHelper 类触发的。
class AddressBeanHelper{
AddressBean bean =null;
AddressBeanHelper(AddressBean bean){
this.bean=bean;
}
ValidationService validate=new ValidateAddressService();
function doStuff(){
validate.preValidateAddressBean (bean);
//business logic for AddressBean
validate.preValidateAddressBean (bean);
}
}
class ValidateAddressService implements ValidationService <AddressBean>{
preValidateAddressBean (AddressBean bean){
//here is the issue
}
preValidateAddressBean (AddressBean bean){
//here is the issue
}
}
我想在 spring 中使用一些框架或技巧,我只需要在验证函数中编写通用代码并将我的验证规则外部化到代码本身之外。就像一个规则引擎,它可以自动验证 bean 的每一个属性。目前我的应用程序基础设施是服务器端的spring/hibernate,客户端的jsp/jquery和部署服务器在heroku上。