IAmYourFaja
IAmYourFaja

Reputation: 56912

Intercepting Specific Annotations with Spring AOP

I'm looking to see whether or not the following is even possible, as all preliminary searches haven't turned back anything to indicate either way.

I'd like to use Hibernate's Validator annotations to validate bean methods, and I would like to use some AOP framework (Spring, AOP Alliance, AspectJ, etc.) to intercept methods annotated with a subset of the Hibernate Validator annotations (@NotNull, @NotEmpty, @Email, etc.); I then want AOP advice to run when they are encountered.

Is this possible to do? If so, I am having a tough time visualizing how the code would work. Using Spring AOP's MethodInterceptor interface as an example:

First, the bean using Hibernate Validator:

public class SomeBean
{
    private String data;

    // Hibernate Validator annotation specifying that "data" cannot be an empty
    // string.
    @NotEmpty
    public String getData() { ... } // etc.
}

Then, some code using that bean:

public void someMethod()
{
    SomeBean oBean = new SomeBean();

    // Validation should fail because we specified that "data" cannot be empty.
    oBean.setData("");
}

Next, the AOP advice to be ran when Hibernate Validator-annotated methods are encountered.

public class ValidationInterceptor implements MethodInterceptor
{
    public Object invoke(MethodInvocation invocation)
    {
        // Here's where we would use Hibernate's validator classes.
        // My code example here is wrong, but it gets the point across.
        Class targetClass = invocation.getClass(); // Should give me SomeBean.class
        ClassValidator<targetClass> oValidator= new ClassValidator<targetClass>();

        // Here I need to get a reference to the instance of the offending
        // SomeBean object whose data has been set to empty...not sure how!
        SomeBean oOffendingBean = getTheBadBeanSomehow();

        InvalidValue[] badVals = oValidator.getInvalidValues(oOffendingBean);
    }
}

So, not only am I choking on what the Spring AOP (pointcut definitions, etc.) configuration would look like to intercept the Hibernate Validator annotations I want, and not only do I not fully grasp how to implement the actual advice (e.g. how to instantiate the offending SomeBean from inside the advice as I mention above in the comments), but I'm not even sure if this solution is possible, Spring or otherwise.

Thanks in advance for some gentle "nudges" in the right direction!

Upvotes: 1

Views: 1344

Answers (1)

Gunnar
Gunnar

Reputation: 18990

You might be interested in the method validation feature introduced with Hibernate Validator 4.2 which provides support for validating method parameters and return values.

You then might use Seam Validation which integrates this functionality with CDI. If you want to use method validation together with Spring you could have a look this project on GitHub which shows how to integrate the method validation functionality with Spring AOP (disclaimer: I'm the author of this project as well as of Seam Validation).

To make your example working you would have to annote the parameter of the setter method with @NotEmpty like this:

public class SomeBean {

    private String data;

    @NotEmpty
    public String getData() { return data; }

    public void setData(@NotEmpty String data) { this.data = data; }

}

Upvotes: 1

Related Questions