Hibernate-validator AOP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.