使用 Spring AOP 拦截特定注解

发布于 2024-12-10 12:39:46 字数 1879 浏览 0 评论 0原文

我正在寻找以下是否可能,因为所有初步搜索都没有返回任何表明任何一种方式的信息。

我想使用 Hibernate 的 Validator 注释来验证 bean 方法,并且我想使用一些 AOP 框架(Spring、AOP Alliance、AspectJ 等)来拦截用 Bean 子集注释的方法Hibernate Validator 注解(@NotNull@NotEmpty@Email 等);然后我希望 AOP 建议在遇到它们时运行。

这可以吗?如果是这样,我很难想象代码将如何工作。以 Spring AOP 的 MethodInterceptor 接口为例:

首先,使用 Hibernate Validator 的 bean:

public class SomeBean
{
    private String data;

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

然后,使用该 bean 的一些代码:

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

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

接下来,遇到 Hibernate Validator 带注释的方法时运行的 AOP 建议。

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);
    }
}

因此,我不仅对 Spring AOP(切入点定义等)配置拦截我想要的 Hibernate Validator 注释感到窒息,而且我不仅没有完全掌握如何实现实际建议(例如如何正如我在上面的评论中提到的,从建议中实例化有问题的 SomeBean ),但我什至不确定这个解决方案是否可行,Spring 还是其他解决方案。

预先感谢您在正确方向上的一些温和的“推动”!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2024-12-17 12:39:46

您可能对 方法验证感兴趣 Hibernate Validator 4.2 中引入的功能,它提供了对验证方法参数和返回值的支持。

然后,您可以使用 Seam Validation 来将此功能与 CDI 集成。如果您想将方法验证与 Spring 一起使用,您可以查看 GitHub 上的 项目,其中显示了如何将方法验证功能与 Spring AOP 集成(免责声明:我是该项目以及 Seam Validation 的作者)。

为了使您的示例正常工作,您必须使用 @NotEmpty 注释 setter 方法的参数,如下所示:

public class SomeBean {

    private String data;

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

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

}

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; }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文