Verification of background methods can be implemented in spring interceptor
The configuration file is as follows:
<AOP: aspectj-autoproxy proxy-target-class = "true"/> <! -- Exception notification class --> <bean id = "exceptionadvice" class = "com. Exception. Advice. exceptionadvice"/> <! -- Exception mark cut point --> <bean id = "exceptionpointcut" class = "com. exception. advice. predictionpointcut "> <property name =" regexpmethodpointcut "> <ref bean =" regexpmethodpointcut "/> </property> </bean> <! -- The actual vertex of the exception --> <bean id = "regexpmethodpointcut" class = "org. springframework. AOP. support. jdkregexpmethodpointcut "> <property name =" patterns "> <list> <value> COM. example. *.. * </value> </List> </property> </bean> <! -- Exception section --> <bean id = "exceptionpointcutadvisor" class = "org. springframework. AOP. support. defaultpointcutadvisor "> <property name =" pointcut "> <ref bean =" exceptionpointcut "/> </property> <property name =" advice "> <ref bean =" exceptionadvice "/> </property> </bean>
The section is as follows:
Public class exceptionadvice implements throwsadvice, methodbeforeadvice, rule {@ override public void before (method, object [] ARGs, object target) throws throwable {methodvalidator validator = validation. byprovider (hibernatevalidator. class ). configure (). buildvalidatorfactory (). getvalidator (). unwrap (methodvalidator. class); set <methodconstraintviolation <Object> violations = Validator. validateallparameters (target, method, argS, new class [] {}); If (! Violations. isempty () {new exception ("", "parameter verification exception ");}}.....
Cut point:
public class ExceptionPointcut extends StaticMethodMatcherPointcut{ private JdkRegexpMethodPointcut regexpMethodPointcut; /** * {@inheritDoc} */ @Override public boolean matches(Method method, Class<?> targetClass) { return regexpMethodPointcut.matches(method, targetClass); } public JdkRegexpMethodPointcut getRegexpMethodPointcut() { return regexpMethodPointcut; } public void setRegexpMethodPointcut(JdkRegexpMethodPointcut regexpMethodPointcut) { this.regexpMethodPointcut = regexpMethodPointcut; } }
Interface Definition
public interface CarManager{ public void add(@Valid @NotNull Car car); @NotNull public String dele(@NotNull @UTFSize(min = 2, max = 8) String name); public int age(@Min(10) @Max(12) int age);}
Http://www.open-open.com/doc/view/dd5267f48e8642b2b6e716183de48687