使用 Spring AOP 拦截特定注解
我正在寻找以下是否可能,因为所有初步搜索都没有返回任何表明任何一种方式的信息。
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能对 方法验证感兴趣 Hibernate Validator 4.2 中引入的功能,它提供了对验证方法参数和返回值的支持。
然后,您可以使用 Seam Validation 来将此功能与 CDI 集成。如果您想将方法验证与 Spring 一起使用,您可以查看 GitHub 上的 此 项目,其中显示了如何将方法验证功能与 Spring AOP 集成(免责声明:我是该项目以及 Seam Validation 的作者)。
为了使您的示例正常工作,您必须使用
@NotEmpty
注释 setter 方法的参数,如下所示: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: