I had a situation before where i wanted to use Spring's Validator interface around my business services. The scenario was i wanted to automatically call the validate method when a service method is invoked.
i am not using Spring MVC, which allows validation at binding level. Anyway, i had to do a workaround & using AOP i tried to proxy each business service & inject the validator on certain method patterns, as shown
The ValidatorProxyFactoryBean actually is responsible for creating the actual proxy. Developer creates the validator i.e. AliasValidator & only is concerned with validations related to the current scenario.Code:<bean id="aliasValidatorDao" class="validation.core.factory.ValidatorProxyFactoryBean"> <property name="target"> <bean class="xxx.xxx.xxxx.OracleAliasDao"> <property name="oracleProcedureSupport"> <ref bean="oracleProcSupport" /></property> </bean> </property> <property name="methodPatterns"><value>insert*,update*</value></property> <property name="validator"> <bean class="xxx.xxxx.xxxxx.xxx.AliasValidator" /> </property> <!--setting this to false will disable the validation --> <property name="validate"><value>true</value></property> <property name="messageTypeHolder"><value>validation.support.StrutsMessagesSupport</value></property> </bean>
Furthermore, StrutsMessagesSupport implements the Errors interface & uses internally ActionMessages.
Anyway, i hope this approach was worth the effort. If you do like what you see or if you think there is a better way of configuring this [maybe through AspectJ], please let me know. Most of us don't have luxury of using Spring MVC, moreover i rather have all validations done on the service side [complex ones]


Reply With Quote